home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / librx / rx.h < prev   
C/C++ Source or Header  |  1994-10-20  |  97KB  |  3,256 lines

  1. #if !defined(RXH) || defined(RX_WANT_SE_DEFS)
  2. #define RXH
  3.  
  4. /*    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  5.  
  6. This file is part of the librx library.
  7.  
  8. Librx is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU Library General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. Librx is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU Library General Public
  19. License along with this software; see the file COPYING.LIB.  If not,
  20. write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA
  21. 02139, USA.  */
  22. /*  t. lord    Wed Sep 23 18:20:57 1992    */
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. #ifndef RX_WANT_SE_DEFS
  32.  
  33. /* This page: Bitsets */
  34.  
  35. #ifndef RX_subset
  36. typedef unsigned int RX_subset;
  37. #define RX_subset_bits    (32)
  38. #define RX_subset_mask    (RX_subset_bits - 1)
  39. #endif
  40.  
  41. typedef RX_subset * rx_Bitset;
  42.  
  43. #ifdef __STDC__
  44. typedef void (*rx_bitset_iterator) (void *, int member_index);
  45. #else
  46. typedef void (*rx_bitset_iterator) ();
  47. #endif
  48.  
  49. #define rx_bitset_subset(N)  ((N) / RX_subset_bits)
  50. #define rx_bitset_subset_val(B,N)  ((B)[rx_bitset_subset(N)])
  51. #define RX_bitset_access(B,N,OP) \
  52.   ((B)[rx_bitset_subset(N)] OP rx_subset_singletons[(N) & RX_subset_mask])
  53. #define RX_bitset_member(B,N)   RX_bitset_access(B, N, &)
  54. #define RX_bitset_enjoin(B,N)   RX_bitset_access(B, N, |=)
  55. #define RX_bitset_remove(B,N)   RX_bitset_access(B, N, &= ~)
  56. #define RX_bitset_toggle(B,N)   RX_bitset_access(B, N, ^= )
  57. #define rx_bitset_numb_subsets(N) (((N) + RX_subset_bits - 1) / RX_subset_bits)
  58. #define rx_sizeof_bitset(N)    (rx_bitset_numb_subsets(N) * sizeof(RX_subset))
  59.  
  60.  
  61.  
  62. /* This page: Splay trees. */
  63.  
  64. #ifdef __STDC__
  65. typedef int (*rx_sp_comparer) (void * a, void * b);
  66. #else
  67. typedef int (*rx_sp_comparer) ();
  68. #endif
  69.  
  70. struct rx_sp_node 
  71. {
  72.   void * key;
  73.   void * data;
  74.   struct rx_sp_node * kids[2];
  75. };
  76.  
  77. #ifdef __STDC__
  78. typedef void (*rx_sp_key_data_freer) (struct rx_sp_node *);
  79. #else
  80. typedef void (*rx_sp_key_data_freer) ();
  81. #endif
  82.  
  83.  
  84. /* giant inflatable hash trees */
  85.  
  86. struct rx_hash_item
  87. {
  88.   struct rx_hash_item * next_same_hash;
  89.   struct rx_hash * table;
  90.   unsigned long hash;
  91.   void * data;
  92.   void * binding;
  93. };
  94.  
  95. struct rx_hash
  96. {
  97.   struct rx_hash * parent;
  98.   int refs;
  99.   struct rx_hash * children[13];
  100.   struct rx_hash_item * buckets [13];
  101.   int bucket_size [13];
  102. };
  103.  
  104. struct rx_hash_rules;
  105.  
  106. #ifdef __STDC__
  107. /* should return like == */
  108. typedef int (*rx_hash_eq)(void *, void *);
  109. typedef struct rx_hash * (*rx_alloc_hash)(struct rx_hash_rules *);
  110. typedef void (*rx_free_hash)(struct rx_hash *,
  111.                 struct rx_hash_rules *);
  112. typedef struct rx_hash_item * (*rx_alloc_hash_item)(struct rx_hash_rules *,
  113.                             void *);
  114. typedef void (*rx_free_hash_item)(struct rx_hash_item *,
  115.                  struct rx_hash_rules *);
  116. #else
  117. typedef int (*rx_hash_eq)();
  118. typedef struct rx_hash * (*rx_alloc_hash)();
  119. typedef void (*rx_free_hash)();
  120. typedef struct rx_hash_item * (*rx_alloc_hash_item)();
  121. typedef void (*rx_free_hash_item)();
  122. #endif
  123.  
  124. struct rx_hash_rules
  125. {
  126.   rx_hash_eq eq;
  127.   rx_alloc_hash hash_alloc;
  128.   rx_free_hash free_hash;
  129.   rx_alloc_hash_item hash_item_alloc;
  130.   rx_free_hash_item free_hash_item;
  131. };
  132.  
  133.  
  134.  
  135. /* Matchers decide what to do by examining a series of these.
  136.  * Instruction types are described below.
  137.  */
  138. struct rx_inx 
  139. {
  140.   void * inx;
  141.   void * data;
  142.   void * data_2;
  143.   void * fnord;
  144. };
  145.  
  146. /* Struct RX holds a compiled regular expression - that is, an nfa ready to be
  147.  * converted on demand to a more efficient nfa.  This is for the low level interface.
  148.  * The high-level interface incloses this in a `struct re_pattern_buffer'.
  149.  */
  150. struct rx_cache;
  151. #ifdef __STDC__
  152. struct rx_se_list;
  153. struct rx;
  154. typedef int (*rx_se_list_order) (struct rx *,
  155.                  struct rx_se_list *, struct rx_se_list *);
  156. #else
  157. typedef int (*rx_se_list_order) ();
  158. #endif
  159.  
  160. struct rx_superset;
  161.  
  162. struct rx
  163. {
  164.   int rx_id;        /* Every edition numbered and signed by eclose_nfa. */
  165.  
  166.   struct rx_cache * cache;  /* Where superstates come from */
  167.  
  168.   /* Every regex defines the size of its own character set. */
  169.   int local_cset_size;
  170.  
  171.   void * buffer;        /* Malloced memory for the nfa. */
  172.   unsigned long allocated;    /* Size of that memory. */
  173.  
  174.   /* How much buffer space to save for external uses.  After compilation,
  175.    * this space will be available at (buffer + allocated - reserved)
  176.    */
  177.   unsigned long reserved;
  178.  
  179.   /* --------- The remaining fields are for internal use only. --------- */
  180.   /* --------- But! they should be initialized to 0.           --------- */
  181.   /* NODEC is the number of nodes in the NFA with non-epsilon
  182.    * orx transitions. 
  183.    */
  184.   int nodec;
  185.  
  186.   /* EPSNODEC is the number of nodes with only epsilon (orx) transitions. */
  187.   int epsnodec;
  188.  
  189.   /* The sum of NODEC & EPSNODEC is the total number of states in the
  190.    * compiled NFA.
  191.    */
  192.  
  193.   /* side_effect_progs temporarily holds a tree of side effect lists. */
  194.   struct rx_hash se_list_memo;
  195.  
  196.   /* A memo for sets of states in the possible_future lists of an nfa: */
  197.   struct rx_hash set_list_memo;
  198.  
  199.   /* The instruction table is indexed by the enum of instructions defined in 
  200.    * rxrun.h.  The values in the table are used to fill in the `inx'
  201.    * slot of instruction frames (see rxrun.h).
  202.    */
  203.   void ** instruction_table;
  204.   struct rx_nfa_state *nfa_states;
  205.   struct rx_nfa_state *start;
  206.  
  207.   /* This orders the search through super-nfa paths. */
  208.   rx_se_list_order se_list_cmp;
  209.  
  210.   struct rx_superset * start_set;
  211. };
  212.  
  213. /* An RX NFA may contain epsilon edges labeled with side effects.
  214.  * These side effects represent match actions that can not normally be
  215.  * defined in a `pure' NFA; for example, recording the location at
  216.  * which a paren is crossed in a register structure.  
  217.  *
  218.  * A matcher is supposed to find a particular path
  219.  * through the NFA (such as leftmost-longest), and then to execute the
  220.  * side effects along that path.  Operationally, the space of paths is
  221.  * searched and side effects are carried out incrementally, and with
  222.  * backtracking.
  223.  *
  224.  * As the NFA is manipulated during matching sets of side effects.
  225.  * Simple lists are used to hold side effect lists. 
  226.  */
  227.  
  228. typedef void * rx_side_effect;
  229.  
  230. struct rx_se_list
  231. {
  232.   rx_side_effect car;
  233.   struct rx_se_list * cdr;
  234. };
  235.  
  236.  
  237.  
  238. /* Struct rexp_node holds an expression tree that represents a regexp.
  239.  * In this expression tree, every node has a type, and some parameters
  240.  * appropriate to that type.
  241.  */
  242.  
  243. enum rexp_node_type
  244. {
  245.   r_cset,            /* Match from a character set. `a' or `[a-z]'*/
  246.   r_concat,            /* Concat two regexps.   `ab' */
  247.   r_alternate,            /* Choose one of two regexps. `a\|b' */
  248.   r_opt,            /* Optional regexp. `a?' */
  249.   r_star,            /* Repeated regexp. `a*' */
  250.   r_2phase_star,        /* hard to explain */
  251.   r_side_effect,        /* Matches the empty string, but
  252.                  * implies that a side effect must
  253.                  * take place.  These nodes are used
  254.                  * by the parser to implement parens,
  255.                  * backreferences etc.
  256.                  */
  257.  
  258.   r_data            /* R_DATA is soley for the convenience
  259.                  * of parsers or other rexp
  260.                  * transformers that want to
  261.                  * (temporarily) introduce new node
  262.                  * types in rexp structures.  These
  263.                  * must be eliminated
  264.                      * by the time build_nfa is called.
  265.                    */
  266. };
  267.  
  268. struct rexp_node
  269. {
  270.   enum rexp_node_type type;
  271.   union
  272.   {
  273.     rx_Bitset cset;
  274.     rx_side_effect side_effect;
  275.     struct
  276.       {
  277.     struct rexp_node *left;
  278.     struct rexp_node *right;
  279.       } pair;
  280.     void * data;
  281.   } params;
  282. };
  283.  
  284.  
  285.  
  286. /* This defines the structure of the NFA into which rexps are compiled. */
  287.  
  288. struct rx_nfa_state
  289. {
  290.   int id;        
  291.   struct rx_nfa_edge *edges;
  292.   struct rx_possible_future *futures;
  293.   unsigned int is_final:1;
  294.   unsigned int is_start:1;
  295.   unsigned int eclosure_needed:1;
  296.   struct rx_nfa_state *next;
  297.   unsigned int mark:1;
  298. };
  299.  
  300. enum rx_nfa_etype
  301. {
  302.   ne_cset,
  303.   ne_epsilon,
  304.   ne_side_effect        /* A special kind of epsilon. */
  305. };
  306.  
  307. struct rx_nfa_edge
  308. {
  309.   struct rx_nfa_edge *next;
  310.   enum rx_nfa_etype type;
  311.   struct rx_nfa_state *dest;
  312.   union
  313.   {
  314.     rx_Bitset cset;
  315.     rx_side_effect side_effect;
  316.   } params;
  317. };
  318.  
  319. struct rx_nfa_state_set
  320. {
  321.   struct rx_nfa_state * car;
  322.   struct rx_nfa_state_set * cdr;
  323. };
  324.  
  325. struct rx_possible_future
  326. {
  327.   struct rx_possible_future *next;
  328.   struct rx_se_list * effects;
  329.   struct rx_nfa_state_set * destset;
  330. };
  331.  
  332.  
  333.  
  334. enum rx_opcode
  335. {
  336.   /* 
  337.    * BACKTRACK_POINT is invoked when a transition results in more
  338.    * than one possible future.
  339.    *
  340.    * There is one occurence of this instruction per transition_class
  341.    * structure; that occurence is only ever executed if the 
  342.    * transition_class contains a list of more than 1 edge.
  343.    */
  344.   rx_backtrack_point = 0,    /* data is (struct transition_class *) */
  345.  
  346.   /* 
  347.    * RX_DO_SIDE_EFFECTS evaluates the side effects of an epsilon path.
  348.    * There is one occurence of this instruction per rx_distinct_future.
  349.    * This instruction is skipped if a rx_distinct_future has no side effects.
  350.    */
  351.   rx_do_side_effects = rx_backtrack_point + 1,
  352.   /* data is (struct rx_distinct_future *) */
  353.  
  354.   /* 
  355.    * RX_CACHE_MISS instructions are stored in rx_distinct_futures whose
  356.    * destination superstate has been reclaimed (or was never built).
  357.    * It recomputes the destination superstate.
  358.    * RX_CACHE_MISS is also stored in a superstate transition table before
  359.    * any of its edges have been built.
  360.    */
  361.   rx_cache_miss = rx_do_side_effects + 1,
  362.   /* data is (struct rx_distinct_future *) */
  363.  
  364.   /* 
  365.    * RX_NEXT_CHAR is called to consume the next character and take the
  366.    * corresponding transition.  This is the only instruction that uses 
  367.    * the DATA field of the instruction frame instead of DATA_2.
  368.    * (see EXPLORE_FUTURE in regex.c).
  369.    */
  370.   rx_next_char = rx_cache_miss + 1, /* data is (struct superstate *) */
  371.  
  372.   /* RX_BACKTRACK indicates that a transition fails.
  373.    */
  374.   rx_backtrack = rx_next_char + 1, /* no data */
  375.  
  376.   /* 
  377.    * RX_ERROR_INX is stored only in places that should never be executed.
  378.    */
  379.   rx_error_inx = rx_backtrack + 1, /* Not supposed to occur. */
  380.  
  381.   rx_num_instructions = rx_error_inx + 1
  382. };
  383.  
  384. /* An id_instruction_table holds the values stored in instruction
  385.  * frames.  The table is indexed by the enums declared above.
  386.  */
  387. extern void * rx_id_instruction_table[rx_num_instructions];
  388.  
  389. #if 0                /* Already declared way above. */
  390. /*  If the instruction is `rx_next_char' then data is valid.  Otherwise it's 0
  391.  *  and data_2 is valid.
  392.  */
  393. struct rx_inx 
  394. {
  395.   void * inx;
  396.   void * data;
  397.   void * data_2;
  398. };
  399. #endif
  400.  
  401.  
  402. #ifndef RX_TAIL_ARRAY
  403. #define RX_TAIL_ARRAY  1
  404. #endif
  405.  
  406. /* A superstate corresponds to a set of nfa states.  Those sets are
  407.  * represented by STRUCT RX_SUPERSET.  The constructors
  408.  * guarantee that only one (shared) structure is created for a given set.
  409.  */
  410. struct rx_superset
  411. {
  412.   int refs;
  413.   struct rx_nfa_state * car;    /* May or may not be a valid addr. */
  414.   int id;            /* == car->id for the initial value of *car */
  415.   struct rx_superset * cdr;    /* May be NULL or a live or semifreed super*/
  416.  
  417.   /* If the corresponding superstate exists: */
  418.   struct rx_superstate * superstate;
  419.  
  420.   /* If this is a starting state (as built by re_search_2)
  421.    * this points to the `struct rx'.  The memory for these objects
  422.    * is typed -- so even after they are freed it is safe to look
  423.    * at this field (to check, in fact, if this was freed.)
  424.    */
  425.   struct rx * starts_for;
  426.  
  427.   struct rx_hash_item hash_item;
  428. };
  429.  
  430. #define rx_protect_superset(RX,CON) (++(CON)->refs)
  431.  
  432. /* Every character occurs in at most one super edge per super-state.
  433.  * But, that edge might have more than one option, indicating a point
  434.  * of non-determinism. 
  435.  */
  436. struct rx_super_edge
  437. {
  438.   struct rx_super_edge *next;
  439.   struct rx_inx rx_backtrack_frame;
  440.   int cset_size;
  441.   rx_Bitset cset;
  442.   struct rx_distinct_future *options;
  443. };
  444.  
  445. /* A superstate is a set of nfa states (RX_SUPERSET) along
  446.  * with a transition table.  Superstates are built on demand and reclaimed
  447.  * without warning.  To protect a superstate, use LOCK_SUPERSTATE.
  448.  *
  449.  * Joe Keane thought of calling these superstates and several people
  450.  * have commented on what a good name it is for what they do. 
  451.  */
  452. struct rx_superstate
  453. {
  454.   int rx_id;
  455.   int locks;
  456.   struct rx_superstate * next_recyclable;
  457.   struct rx_superstate * prev_recyclable;
  458.   struct rx_distinct_future * transition_refs;
  459.   struct rx_superset * contents;
  460.   struct rx_super_edge * edges;
  461.   int is_semifree;
  462.   int trans_size;
  463.   struct rx_inx transitions[RX_TAIL_ARRAY]; /* cset sized */
  464. };
  465.  
  466. struct rx_distinct_future
  467. {
  468.   struct rx_distinct_future * next_same_super_edge[2];
  469.   struct rx_distinct_future * next_same_dest;
  470.   struct rx_distinct_future * prev_same_dest;
  471.   struct rx_superstate * present;    /* source state */
  472.   struct rx_superstate * future;    /* destination state */
  473.   struct rx_super_edge * edge;
  474.   struct rx_inx future_frame;
  475.   struct rx_inx side_effects_frame;
  476.   struct rx_se_list * effects;
  477. };
  478.  
  479. #define rx_lock_superstate(R,S)  ((S)->locks++)
  480. #define rx_unlock_superstate(R,S) (--(S)->locks)
  481.  
  482.  
  483. /* This page destined for rx.h */
  484.  
  485. struct rx_blocklist
  486. {
  487.   struct rx_blocklist * next;
  488.   int bytes;
  489. };
  490.  
  491. struct rx_freelist
  492. {
  493.   struct rx_freelist * next;
  494. };
  495.  
  496. struct rx_cache;
  497.  
  498. #ifdef __STDC__
  499. typedef void (*rx_morecore_fn)(struct rx_cache *);
  500. #else
  501. typedef void (*rx_morecore_fn)();
  502. #endif
  503.  
  504. /* You use this to control the allocation of superstate data 
  505.  * during matching.  Most of it should be initialized to 0.
  506.  *
  507.  * A MORECORE function is necessary.  It should allocate
  508.  * a new block of memory or return 0.
  509.  * A default that uses malloc is called `rx_morecore'.
  510.  *
  511.  * The number of SUPERSTATES_ALLOWED indirectly limits how much memory
  512.  * the system will try to allocate.  The default is 128.  Batch style
  513.  * applications that are very regexp intensive should use as high a number
  514.  * as possible without thrashing.
  515.  * 
  516.  * The LOCAL_CSET_SIZE is the number of characters in a character set.
  517.  * It is therefore the number of entries in a superstate transition table.
  518.  * Generally, it should be 256.  If your character set has 16 bits, 
  519.  * it is better to translate your regexps into equivalent 8 bit patterns.
  520.  */
  521.  
  522. struct rx_cache
  523. {
  524.   struct rx_hash_rules superset_hash_rules;
  525.  
  526.   /* Objects are allocated by incrementing a pointer that 
  527.    * scans across rx_blocklists.
  528.    */
  529.   struct rx_blocklist * memory;
  530.   struct rx_blocklist * memory_pos;
  531.   int bytes_left;
  532.   char * memory_addr;
  533.   rx_morecore_fn morecore;
  534.  
  535.   /* Freelists. */
  536.   struct rx_freelist * free_superstates;
  537.   struct rx_freelist * free_transition_classes;
  538.   struct rx_freelist * free_discernable_futures;
  539.   struct rx_freelist * free_supersets;
  540.   struct rx_freelist * free_hash;
  541.  
  542.   /* Two sets of superstates -- those that are semifreed, and those
  543.    * that are being used.
  544.    */
  545.   struct rx_superstate * lru_superstate;
  546.   struct rx_superstate * semifree_superstate;
  547.  
  548.   struct rx_superset * empty_superset;
  549.  
  550.   int superstates;
  551.   int semifree_superstates;
  552.   int hits;
  553.   int misses;
  554.   int superstates_allowed;
  555.  
  556.   int local_cset_size;
  557.   void ** instruction_table;
  558.  
  559.   struct rx_hash superset_table;
  560. };
  561.  
  562.  
  563.  
  564. /* The lowest-level search function supports arbitrarily fragmented
  565.  * strings and (optionally) suspendable/resumable searches.
  566.  *
  567.  * Callers have to provide a few hooks.
  568.  */
  569.  
  570. #ifndef __GNUC__
  571. #ifdef __STDC__
  572. #define __const__ const
  573. #else
  574. #define __const__
  575. #endif
  576. #endif
  577.  
  578. /* This holds a matcher position */
  579. struct rx_string_position
  580. {
  581.   __const__ unsigned char * pos;    /* The current pos. */
  582.   __const__ unsigned char * string; /* The current string burst. */
  583.   __const__ unsigned char * end;    /* First invalid position >= POS. */
  584.   int offset;            /* Integer address of the current burst. */
  585.   int size;            /* Current string's size. */
  586.   int search_direction;        /* 1 or -1 */
  587.   int search_end;        /* First position to not try. */
  588. };
  589.  
  590.  
  591. enum rx_get_burst_return
  592. {
  593.   rx_get_burst_continuation,
  594.   rx_get_burst_error,
  595.   rx_get_burst_ok,
  596.   rx_get_burst_no_more
  597. };
  598.  
  599.  
  600. /* A call to get burst should make POS valid.  It might be invalid
  601.  * if the STRING field doesn't point to a burst that actually
  602.  * contains POS.
  603.  *
  604.  * GET_BURST should take a clue from SEARCH_DIRECTION (1 or -1) as to
  605.  * whether or not to pad to the left.  Padding to the right is always
  606.  * appropriate, but need not go past the point indicated by STOP.
  607.  *
  608.  * If a continuation is returned, then the reentering call to
  609.  * a search function will retry the get_burst.
  610.  */
  611.  
  612. #ifdef __STDC__
  613. typedef enum rx_get_burst_return
  614.   (*rx_get_burst_fn) (struct rx_string_position * pos,
  615.               void * app_closure,
  616.               int stop);
  617.                            
  618. #else
  619. typedef enum rx_get_burst_return (*rx_get_burst_fn) ();
  620. #endif
  621.  
  622.  
  623. enum rx_back_check_return
  624. {
  625.   rx_back_check_continuation,
  626.   rx_back_check_error,
  627.   rx_back_check_pass,
  628.   rx_back_check_fail
  629. };
  630.  
  631. /* Back_check should advance the position it is passed 
  632.  * over rparen - lparen characters and return pass iff
  633.  * the characters starting at POS match those indexed
  634.  * by [LPAREN..RPAREN].
  635.  *
  636.  * If a continuation is returned, then the reentering call to
  637.  * a search function will retry the back_check.
  638.  */
  639.  
  640. #ifdef __STDC__
  641. typedef enum rx_back_check_return
  642.   (*rx_back_check_fn) (struct rx_string_position * pos,
  643.                int lparen,
  644.                int rparen,
  645.                unsigned char * translate,
  646.                void * app_closure,
  647.                int stop);
  648.                            
  649. #else
  650. typedef enum rx_back_check_return (*rx_back_check_fn) ();
  651. #endif
  652.  
  653.  
  654.  
  655.  
  656. /* A call to fetch_char should return the character at POS or POS + 1.
  657.  * Returning continuations here isn't supported.  OFFSET is either 0 or 1
  658.  * and indicates which characters is desired.
  659.  */
  660.  
  661. #ifdef __STDC__
  662. typedef int (*rx_fetch_char_fn) (struct rx_string_position * pos,
  663.                  int offset,
  664.                  void * app_closure,
  665.                  int stop);
  666. #else
  667. typedef int (*rx_fetch_char_fn) ();
  668. #endif
  669.  
  670.  
  671. enum rx_search_return
  672. {
  673.   rx_search_continuation = -4,
  674.   rx_search_error = -3,
  675.   rx_search_soft_fail = -2,    /* failed by running out of string */
  676.   rx_search_fail = -1        /* failed only by reaching failure states */
  677.   /* return values >= 0 indicate the position of a successful match */
  678. };
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685. /* regex.h
  686.  * 
  687.  * The remaining declarations replace regex.h.
  688.  */
  689.  
  690. /* This is an array of error messages corresponding to the error codes.
  691.  */
  692. extern __const__ char *re_error_msg[];
  693.  
  694. /* If any error codes are removed, changed, or added, update the
  695.    `re_error_msg' table in regex.c.  */
  696. typedef enum
  697. {
  698.   REG_NOERROR = 0,    /* Success.  */
  699.   REG_NOMATCH,        /* Didn't find a match (for regexec).  */
  700.  
  701.   /* POSIX regcomp return error codes.  (In the order listed in the
  702.      standard.)  */
  703.   REG_BADPAT,        /* Invalid pattern.  */
  704.   REG_ECOLLATE,        /* Not implemented.  */
  705.   REG_ECTYPE,        /* Invalid character class name.  */
  706.   REG_EESCAPE,        /* Trailing backslash.  */
  707.   REG_ESUBREG,        /* Invalid back reference.  */
  708.   REG_EBRACK,        /* Unmatched left bracket.  */
  709.   REG_EPAREN,        /* Parenthesis imbalance.  */ 
  710.   REG_EBRACE,        /* Unmatched \{.  */
  711.   REG_BADBR,        /* Invalid contents of \{\}.  */
  712.   REG_ERANGE,        /* Invalid range end.  */
  713.   REG_ESPACE,        /* Ran out of memory.  */
  714.   REG_BADRPT,        /* No preceding re for repetition op.  */
  715.  
  716.   /* Error codes we've added.  */
  717.   REG_EEND,        /* Premature end.  */
  718.   REG_ESIZE,        /* Compiled pattern bigger than 2^16 bytes.  */
  719.   REG_ERPAREN        /* Unmatched ) or \); not returned from regcomp.  */
  720. } reg_errcode_t;
  721.  
  722. /* The regex.c support, as a client of rx, defines a set of possible
  723.  * side effects that can be added to the edge lables of nfa edges.
  724.  * Here is the list of sidef effects in use.
  725.  */
  726.  
  727. enum re_side_effects
  728. {
  729. #define RX_WANT_SE_DEFS 1
  730. #undef RX_DEF_SE
  731. #undef RX_DEF_CPLX_SE
  732. #define RX_DEF_SE(IDEM, NAME, VALUE)          NAME VALUE,
  733. #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE)     NAME VALUE,
  734. #include "rx.h"
  735. #undef RX_DEF_SE
  736. #undef RX_DEF_CPLX_SE
  737. #undef RX_WANT_SE_DEFS
  738.    re_floogle_flap = 65533
  739. };
  740.  
  741. /* These hold paramaters for the kinds of side effects that are possible
  742.  * in the supported pattern languages.  These include things like the 
  743.  * numeric bounds of {} operators and the index of paren registers for 
  744.  * subexpression measurement or backreferencing.
  745.  */
  746. struct re_se_params
  747. {
  748.   enum re_side_effects se;
  749.   int op1;
  750.   int op2;
  751. };
  752.  
  753. typedef unsigned reg_syntax_t;
  754.  
  755. struct re_pattern_buffer
  756. {
  757.   struct rx rx;
  758.   reg_syntax_t syntax;        /* See below for syntax bit definitions. */
  759.  
  760.   unsigned int no_sub:1;    /* If set, don't  return register offsets. */
  761.   unsigned int not_bol:1;    /* If set, the anchors ('^' and '$') don't */
  762.   unsigned int not_eol:1;    /*     match at the ends of the string.  */  
  763.   unsigned int newline_anchor:1;/* If true, an anchor at a newline matches.*/
  764.   unsigned int least_subs:1;    /* If set, and returning registers, return
  765.                  * as few values as possible.  Only 
  766.                  * backreferenced groups and group 0 (the whole
  767.                  * match) will be returned.
  768.                  */
  769.  
  770.   /* If true, this says that the matcher should keep registers on its
  771.    * backtracking stack.  For many patterns, we can easily determine that
  772.    * this isn't necessary.
  773.    */
  774.   unsigned int match_regs_on_stack:1;
  775.   unsigned int search_regs_on_stack:1;
  776.  
  777.   /* is_anchored and begbuf_only are filled in by rx_compile. */
  778.   unsigned int is_anchored:1;    /* Anchorded by ^? */
  779.   unsigned int begbuf_only:1;    /* Anchored to char position 0? */
  780.  
  781.   
  782.   /* If REGS_UNALLOCATED, allocate space in the `regs' structure
  783.    * for `max (RE_NREGS, re_nsub + 1)' groups.
  784.    * If REGS_REALLOCATE, reallocate space if necessary.
  785.    * If REGS_FIXED, use what's there.  
  786.    */
  787. #define REGS_UNALLOCATED 0
  788. #define REGS_REALLOCATE 1
  789. #define REGS_FIXED 2
  790.   unsigned int regs_allocated:2;
  791.  
  792.   
  793.   /* Either a translate table to apply to all characters before
  794.    * comparing them, or zero for no translation.  The translation
  795.    * is applied to a pattern when it is compiled and to a string
  796.    * when it is matched.
  797.    */
  798.   unsigned char * translate;
  799.  
  800.   /* If this is a valid pointer, it tells rx not to store the extents of 
  801.    * certain subexpressions (those corresponding to non-zero entries).
  802.    * Passing 0x1 is the same as passing an array of all ones.  Passing 0x0
  803.    * is the same as passing an array of all zeros.
  804.    * The array should contain as many entries as their are subexps in the 
  805.    * regexp.
  806.    */
  807.   char * syntax_parens;
  808.  
  809.     /* Number of subexpressions found by the compiler.  */
  810.   size_t re_nsub;
  811.  
  812.   void * buffer;        /* Malloced memory for the nfa. */
  813.   unsigned long allocated;    /* Size of that memory. */
  814.  
  815.   /* Pointer to a fastmap, if any, otherwise zero.  re_search uses
  816.    * the fastmap, if there is one, to skip over impossible
  817.    * starting points for matches.  */
  818.   char *fastmap;
  819.  
  820.   unsigned int fastmap_accurate:1; /* These three are internal. */
  821.   unsigned int can_match_empty:1;  
  822.   struct rx_nfa_state * start;    /* The nfa starting state. */
  823.  
  824.   /* This is the list of iterator bounds for {lo,hi} constructs.
  825.    * The memory pointed to is part of the rx->buffer.
  826.    */
  827.   struct re_se_params *se_params;
  828.  
  829.   /* This is a bitset representation of the fastmap.
  830.    * This is a true fastmap that already takes the translate
  831.    * table into account.
  832.    */
  833.   rx_Bitset fastset;
  834. };
  835.  
  836. /* Type for byte offsets within the string.  POSIX mandates this.  */
  837. typedef int regoff_t;
  838.  
  839. /* This is the structure we store register match data in.  See
  840.    regex.texinfo for a full description of what registers match.  */
  841. struct re_registers
  842. {
  843.   unsigned num_regs;
  844.   regoff_t *start;
  845.   regoff_t *end;
  846. };
  847.  
  848. typedef struct re_pattern_buffer regex_t;
  849.  
  850. /* POSIX specification for registers.  Aside from the different names than
  851.    `re_registers', POSIX uses an array of structures, instead of a
  852.    structure of arrays.  */
  853. typedef struct
  854. {
  855.   regoff_t rm_so;  /* Byte offset from string's start to substring's start.  */
  856.   regoff_t rm_eo;  /* Byte offset from string's start to substring's end.  */
  857. } regmatch_t;
  858.  
  859.  
  860. /* The following bits are used to determine the regexp syntax we
  861.    recognize.  The set/not-set meanings are chosen so that Emacs syntax
  862.    remains the value 0.  The bits are given in alphabetical order, and
  863.    the definitions shifted by one from the previous bit; thus, when we
  864.    add or remove a bit, only one other definition need change.  */
  865.  
  866. /* If this bit is not set, then \ inside a bracket expression is literal.
  867.    If set, then such a \ quotes the following character.  */
  868. #define RE_BACKSLASH_ESCAPE_IN_LISTS (1)
  869.  
  870. /* If this bit is not set, then + and ? are operators, and \+ and \? are
  871.      literals. 
  872.    If set, then \+ and \? are operators and + and ? are literals.  */
  873. #define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
  874.  
  875. /* If this bit is set, then character classes are supported.  They are:
  876.      [:alpha:], [:upper:], [:lower:],  [:digit:], [:alnum:], [:xdigit:],
  877.      [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
  878.    If not set, then character classes are not supported.  */
  879. #define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
  880.  
  881. /* If this bit is set, then ^ and $ are always anchors (outside bracket
  882.      expressions, of course).
  883.    If this bit is not set, then it depends:
  884.         ^  is an anchor if it is at the beginning of a regular
  885.            expression or after an open-group or an alternation operator;
  886.         $  is an anchor if it is at the end of a regular expression, or
  887.            before a close-group or an alternation operator.  
  888.  
  889.    This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
  890.    POSIX draft 11.2 says that * etc. in leading positions is undefined.
  891.    We already implemented a previous draft which made those constructs
  892.    invalid, though, so we haven't changed the code back.  */
  893. #define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
  894.  
  895. /* If this bit is set, then special characters are always special
  896.      regardless of where they are in the pattern.
  897.    If this bit is not set, then special characters are special only in
  898.      some contexts; otherwise they are ordinary.  Specifically, 
  899.      * + ? and intervals are only special when not after the beginning,
  900.      open-group, or alternation operator.  */
  901. #define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
  902.  
  903. /* If this bit is set, then *, +, ?, and { cannot be first in an re or
  904.      immediately after an alternation or begin-group operator.  */
  905. #define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
  906.  
  907. /* If this bit is set, then . matches newline.
  908.    If not set, then it doesn't.  */
  909. #define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
  910.  
  911. /* If this bit is set, then . doesn't match NUL.
  912.    If not set, then it does.  */
  913. #define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
  914.  
  915. /* If this bit is set, nonmatching lists [^...] do not match newline.
  916.    If not set, they do.  */
  917. #define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
  918.  
  919. /* If this bit is set, either \{...\} or {...} defines an
  920.      interval, depending on RE_NO_BK_BRACES. 
  921.    If not set, \{, \}, {, and } are literals.  */
  922. #define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
  923.  
  924. /* If this bit is set, +, ? and | aren't recognized as operators.
  925.    If not set, they are.  */
  926. #define RE_LIMITED_OPS (RE_INTERVALS << 1)
  927.  
  928. /* If this bit is set, newline is an alternation operator.
  929.    If not set, newline is literal.  */
  930. #define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
  931.  
  932. /* If this bit is set, then `{...}' defines an interval, and \{ and \}
  933.      are literals.
  934.   If not set, then `\{...\}' defines an interval.  */
  935. #define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
  936.  
  937. /* If this bit is set, (...) defines a group, and \( and \) are literals.
  938.    If not set, \(...\) defines a group, and ( and ) are literals.  */
  939. #define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
  940.  
  941. /* If this bit is set, then \<digit> matches <digit>.
  942.    If not set, then \<digit> is a back-reference.  */
  943. #define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
  944.  
  945. /* If this bit is set, then | is an alternation operator, and \| is literal. 
  946.    If not set, then \| is an alternation operator, and | is literal.  */
  947. #define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
  948.  
  949. /* If this bit is set, then an ending range point collating higher
  950.      than the starting range point, as in [z-a], is invalid.
  951.    If not set, then when ending range point collates higher than the
  952.      starting range point, the range is ignored.  */
  953. #define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
  954.  
  955. /* If this bit is set, then an unmatched ) is ordinary.
  956.    If not set, then an unmatched ) is invalid.  */
  957. #define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
  958.  
  959. /* This global variable defines the particular regexp syntax to use (for
  960.    some interfaces).  When a regexp is compiled, the syntax used is
  961.    stored in the pattern buffer, so changing this does not affect
  962.    already-compiled regexps.  */
  963. extern reg_syntax_t re_syntax_options;
  964.  
  965. /* Define combinations of the above bits for the standard possibilities.
  966.    (The [[[ comments delimit what gets put into the Texinfo file, so
  967.    don't delete them!)  */ 
  968. /* [[[begin syntaxes]]] */
  969. #define RE_SYNTAX_EMACS 0
  970.  
  971. #define RE_SYNTAX_AWK                            \
  972.   (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL            \
  973.    | RE_NO_BK_PARENS            | RE_NO_BK_REFS                \
  974.    | RE_NO_BK_VAR               | RE_NO_EMPTY_RANGES            \
  975.    | RE_UNMATCHED_RIGHT_PAREN_ORD)
  976.  
  977. #define RE_SYNTAX_POSIX_AWK                         \
  978.   (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS)
  979.  
  980. #define RE_SYNTAX_GREP                            \
  981.   (RE_BK_PLUS_QM              | RE_CHAR_CLASSES                \
  982.    | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS                \
  983.    | RE_NEWLINE_ALT)
  984.  
  985. #define RE_SYNTAX_EGREP                            \
  986.   (RE_CHAR_CLASSES        | RE_CONTEXT_INDEP_ANCHORS            \
  987.    | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE            \
  988.    | RE_NEWLINE_ALT       | RE_NO_BK_PARENS                \
  989.    | RE_NO_BK_VBAR)
  990.  
  991. #define RE_SYNTAX_POSIX_EGREP                        \
  992.   (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
  993.  
  994. #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
  995.  
  996. /* Syntax bits common to both basic and extended POSIX regex syntax.  */
  997. #define _RE_SYNTAX_POSIX_COMMON                        \
  998.   (RE_CHAR_CLASSES | RE_DOT_NEWLINE      | RE_DOT_NOT_NULL        \
  999.    | RE_INTERVALS  | RE_NO_EMPTY_RANGES)
  1000.  
  1001. #define RE_SYNTAX_POSIX_BASIC                        \
  1002.   (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
  1003.  
  1004. /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
  1005.    RE_LIMITED_OPS, i.e., \? \+ \| are not recognized.  Actually, this
  1006.    isn't minimal, since other operators, such as \`, aren't disabled.  */
  1007. #define RE_SYNTAX_POSIX_MINIMAL_BASIC                    \
  1008.   (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
  1009.  
  1010. #define RE_SYNTAX_POSIX_EXTENDED                    \
  1011.   (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS            \
  1012.    | RE_CONTEXT_INDEP_OPS  | RE_NO_BK_BRACES                \
  1013.    | RE_NO_BK_PARENS       | RE_NO_BK_VBAR                \
  1014.    | RE_UNMATCHED_RIGHT_PAREN_ORD)
  1015.  
  1016. /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS
  1017.    replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added.  */
  1018. #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED                \
  1019.   (_RE_SYNTAX_POSIX_COMMON  | RE_CONTEXT_INDEP_ANCHORS            \
  1020.    | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES                \
  1021.    | RE_NO_BK_PARENS        | RE_NO_BK_REFS                \
  1022.    | RE_NO_BK_VBAR        | RE_UNMATCHED_RIGHT_PAREN_ORD)
  1023. /* [[[end syntaxes]]] */
  1024.  
  1025. /* Maximum number of duplicates an interval can allow.  Some systems
  1026.    (erroneously) define this in other header files, but we want our
  1027.    value, so remove any previous define.  */
  1028. #ifdef RE_DUP_MAX
  1029. #undef RE_DUP_MAX
  1030. #endif
  1031. #define RE_DUP_MAX ((1 << 15) - 1) 
  1032.  
  1033.  
  1034.  
  1035. /* POSIX `cflags' bits (i.e., information for `regcomp').  */
  1036.  
  1037. /* If this bit is set, then use extended regular expression syntax.
  1038.    If not set, then use basic regular expression syntax.  */
  1039. #define REG_EXTENDED 1
  1040.  
  1041. /* If this bit is set, then ignore case when matching.
  1042.    If not set, then case is significant.  */
  1043. #define REG_ICASE (REG_EXTENDED << 1)
  1044.  
  1045. /* If this bit is set, then anchors do not match at newline
  1046.      characters in the string.
  1047.    If not set, then anchors do match at newlines.  */
  1048. #define REG_NEWLINE (REG_ICASE << 1)
  1049.  
  1050. /* If this bit is set, then report only success or fail in regexec.
  1051.    If not set, then returns differ between not matching and errors.  */
  1052. #define REG_NOSUB (REG_NEWLINE << 1)
  1053.  
  1054.  
  1055. /* POSIX `eflags' bits (i.e., information for regexec).  */
  1056.  
  1057. /* If this bit is set, then the beginning-of-line operator doesn't match
  1058.      the beginning of the string (presumably because it's not the
  1059.      beginning of a line).
  1060.    If not set, then the beginning-of-line operator does match the
  1061.      beginning of the string.  */
  1062. #define REG_NOTBOL 1
  1063.  
  1064. /* Like REG_NOTBOL, except for the end-of-line.  */
  1065. #define REG_NOTEOL (1 << 1)
  1066.  
  1067. /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
  1068.  * `re_match_2' returns information about at least this many registers
  1069.  * the first time a `regs' structure is passed. 
  1070.  *
  1071.  * Also, this is the greatest number of backreferenced subexpressions
  1072.  * allowed in a pattern being matched without caller-supplied registers.
  1073.  */
  1074. #ifndef RE_NREGS
  1075. #define RE_NREGS 30
  1076. #endif
  1077.  
  1078. extern int rx_cache_bound;
  1079. extern char rx_version_string[];
  1080.  
  1081.  
  1082.  
  1083. #ifdef RX_WANT_RX_DEFS
  1084.  
  1085. /* This is decls to the interesting subsystems and lower layers
  1086.  * of rx.  Everything which doesn't have a public counterpart in 
  1087.  * regex.c is declared here.
  1088.  */
  1089.  
  1090.  
  1091. #ifdef __STDC__
  1092. typedef void (*rx_hash_freefn) (struct rx_hash_item * it);
  1093. #else /* ndef __STDC__ */
  1094. typedef void (*rx_hash_freefn) ();
  1095. #endif /* ndef __STDC__ */
  1096.  
  1097.  
  1098.  
  1099.  
  1100. #ifdef __STDC__
  1101. RX_DECL int rx_bitset_is_equal (int size, rx_Bitset a, rx_Bitset b);
  1102. RX_DECL int rx_bitset_is_subset (int size, rx_Bitset a, rx_Bitset b);
  1103. RX_DECL int rx_bitset_empty (int size, rx_Bitset set);
  1104. RX_DECL void rx_bitset_null (int size, rx_Bitset b);
  1105. RX_DECL void rx_bitset_universe (int size, rx_Bitset b);
  1106. RX_DECL void rx_bitset_complement (int size, rx_Bitset b);
  1107. RX_DECL void rx_bitset_assign (int size, rx_Bitset a, rx_Bitset b);
  1108. RX_DECL void rx_bitset_union (int size, rx_Bitset a, rx_Bitset b);
  1109. RX_DECL void rx_bitset_intersection (int size,
  1110.                      rx_Bitset a, rx_Bitset b);
  1111. RX_DECL void rx_bitset_difference (int size, rx_Bitset a, rx_Bitset b);
  1112. RX_DECL void rx_bitset_revdifference (int size,
  1113.                       rx_Bitset a, rx_Bitset b);
  1114. RX_DECL void rx_bitset_xor (int size, rx_Bitset a, rx_Bitset b);
  1115. RX_DECL unsigned long rx_bitset_hash (int size, rx_Bitset b);
  1116. RX_DECL struct rx_hash_item * rx_hash_find (struct rx_hash * table,
  1117.                         unsigned long hash,
  1118.                         void * value,
  1119.                         struct rx_hash_rules * rules);
  1120. RX_DECL struct rx_hash_item * rx_hash_store (struct rx_hash * table,
  1121.                          unsigned long hash,
  1122.                          void * value,
  1123.                          struct rx_hash_rules * rules);
  1124. RX_DECL void rx_hash_free (struct rx_hash_item * it, struct rx_hash_rules * rules);
  1125. RX_DECL void rx_free_hash_table (struct rx_hash * tab, rx_hash_freefn freefn,
  1126.                  struct rx_hash_rules * rules);
  1127. RX_DECL rx_Bitset rx_cset (struct rx *rx);
  1128. RX_DECL rx_Bitset rx_copy_cset (struct rx *rx, rx_Bitset a);
  1129. RX_DECL void rx_free_cset (struct rx * rx, rx_Bitset c);
  1130. RX_DECL struct rexp_node * rexp_node (struct rx *rx,
  1131.                       enum rexp_node_type type);
  1132. RX_DECL struct rexp_node * rx_mk_r_cset (struct rx * rx,
  1133.                      rx_Bitset b);
  1134. RX_DECL struct rexp_node * rx_mk_r_concat (struct rx * rx,
  1135.                        struct rexp_node * a,
  1136.                        struct rexp_node * b);
  1137. RX_DECL struct rexp_node * rx_mk_r_alternate (struct rx * rx,
  1138.                           struct rexp_node * a,
  1139.                           struct rexp_node * b);
  1140. RX_DECL struct rexp_node * rx_mk_r_opt (struct rx * rx,
  1141.                     struct rexp_node * a);
  1142. RX_DECL struct rexp_node * rx_mk_r_star (struct rx * rx,
  1143.                      struct rexp_node * a);
  1144. RX_DECL struct rexp_node * rx_mk_r_2phase_star (struct rx * rx,
  1145.                         struct rexp_node * a,
  1146.                         struct rexp_node * b);
  1147. RX_DECL struct rexp_node * rx_mk_r_side_effect (struct rx * rx,
  1148.                         rx_side_effect a);
  1149. RX_DECL struct rexp_node * rx_mk_r_data  (struct rx * rx,
  1150.                       void * a);
  1151. RX_DECL void rx_free_rexp (struct rx * rx, struct rexp_node * node);
  1152. RX_DECL struct rexp_node * rx_copy_rexp (struct rx *rx,
  1153.                      struct rexp_node *node);
  1154. RX_DECL struct rx_nfa_state * rx_nfa_state (struct rx *rx);
  1155. RX_DECL void rx_free_nfa_state (struct rx_nfa_state * n);
  1156. RX_DECL struct rx_nfa_state * rx_id_to_nfa_state (struct rx * rx,
  1157.                           int id);
  1158. RX_DECL struct rx_nfa_edge * rx_nfa_edge (struct rx *rx,
  1159.                       enum rx_nfa_etype type,
  1160.                       struct rx_nfa_state *start,
  1161.                       struct rx_nfa_state *dest);
  1162. RX_DECL void rx_free_nfa_edge (struct rx_nfa_edge * e);
  1163. RX_DECL void rx_free_nfa (struct rx *rx);
  1164. RX_DECL int rx_build_nfa (struct rx *rx,
  1165.               struct rexp_node *rexp,
  1166.               struct rx_nfa_state **start,
  1167.               struct rx_nfa_state **end);
  1168. RX_DECL void rx_name_nfa_states (struct rx *rx);
  1169. RX_DECL int rx_eclose_nfa (struct rx *rx);
  1170. RX_DECL void rx_delete_epsilon_transitions (struct rx *rx);
  1171. RX_DECL int rx_compactify_nfa (struct rx *rx,
  1172.                    void **mem, unsigned long *size);
  1173. RX_DECL void rx_release_superset (struct rx *rx,
  1174.                   struct rx_superset *set);
  1175. RX_DECL struct rx_superset * rx_superset_cons (struct rx * rx,
  1176.                            struct rx_nfa_state *car, struct rx_superset *cdr);
  1177. RX_DECL struct rx_superset * rx_superstate_eclosure_union
  1178.   (struct rx * rx, struct rx_superset *set, struct rx_nfa_state_set *ecl);
  1179. RX_DECL struct rx_superstate * rx_superstate (struct rx *rx,
  1180.                           struct rx_superset *set);
  1181. RX_DECL struct rx_inx * rx_handle_cache_miss
  1182.   (struct rx *rx, struct rx_superstate *super, unsigned char chr, void *data);
  1183. RX_DECL reg_errcode_t rx_compile (__const__ char *pattern, int size,
  1184.                   reg_syntax_t syntax,
  1185.                   struct re_pattern_buffer * rxb);
  1186. RX_DECL void rx_blow_up_fastmap (struct re_pattern_buffer * rxb);
  1187. #else /* STDC */
  1188. RX_DECL int rx_bitset_is_equal ();
  1189. RX_DECL int rx_bitset_is_subset ();
  1190. RX_DECL int rx_bitset_empty ();
  1191. RX_DECL void rx_bitset_null ();
  1192. RX_DECL void rx_bitset_universe ();
  1193. RX_DECL void rx_bitset_complement ();
  1194. RX_DECL void rx_bitset_assign ();
  1195. RX_DECL void rx_bitset_union ();
  1196. RX_DECL void rx_bitset_intersection ();
  1197. RX_DECL void rx_bitset_difference ();
  1198. RX_DECL void rx_bitset_revdifference ();
  1199. RX_DECL void rx_bitset_xor ();
  1200. RX_DECL unsigned long rx_bitset_hash ();
  1201. RX_DECL struct rx_hash_item * rx_hash_find ();
  1202. RX_DECL struct rx_hash_item * rx_hash_store ();
  1203. RX_DECL void rx_hash_free ();
  1204. RX_DECL void rx_free_hash_table ();
  1205. RX_DECL rx_Bitset rx_cset ();
  1206. RX_DECL rx_Bitset rx_copy_cset ();
  1207. RX_DECL void rx_free_cset ();
  1208. RX_DECL struct rexp_node * rexp_node ();
  1209. RX_DECL struct rexp_node * rx_mk_r_cset ();
  1210. RX_DECL struct rexp_node * rx_mk_r_concat ();
  1211. RX_DECL struct rexp_node * rx_mk_r_alternate ();
  1212. RX_DECL struct rexp_node * rx_mk_r_opt ();
  1213. RX_DECL struct rexp_node * rx_mk_r_star ();
  1214. RX_DECL struct rexp_node * rx_mk_r_2phase_star ();
  1215. RX_DECL struct rexp_node * rx_mk_r_side_effect ();
  1216. RX_DECL struct rexp_node * rx_mk_r_data  ();
  1217. RX_DECL void rx_free_rexp ();
  1218. RX_DECL struct rexp_node * rx_copy_rexp ();
  1219. RX_DECL struct rx_nfa_state * rx_nfa_state ();
  1220. RX_DECL void rx_free_nfa_state ();
  1221. RX_DECL struct rx_nfa_state * rx_id_to_nfa_state ();
  1222. RX_DECL struct rx_nfa_edge * rx_nfa_edge ();
  1223. RX_DECL void rx_free_nfa_edge ();
  1224. RX_DECL void rx_free_nfa ();
  1225. RX_DECL int rx_build_nfa ();
  1226. RX_DECL void rx_name_nfa_states ();
  1227. RX_DECL int rx_eclose_nfa ();
  1228. RX_DECL void rx_delete_epsilon_transitions ();
  1229. RX_DECL int rx_compactify_nfa ();
  1230. RX_DECL void rx_release_superset ();
  1231. RX_DECL struct rx_superset * rx_superset_cons ();
  1232. RX_DECL struct rx_superset * rx_superstate_eclosure_union ();
  1233. RX_DECL struct rx_superstate * rx_superstate ();
  1234. RX_DECL struct rx_inx * rx_handle_cache_miss ();
  1235. RX_DECL reg_errcode_t rx_compile ();
  1236. RX_DECL void rx_blow_up_fastmap ();
  1237. #endif /* STDC */
  1238.  
  1239.  
  1240. #endif /* RX_WANT_RX_DEFS */
  1241.  
  1242.  
  1243.  
  1244. #ifdef __STDC__
  1245. extern int re_search_2 (struct re_pattern_buffer *rxb,
  1246.             __const__ char * string1, int size1,
  1247.             __const__ char * string2, int size2,
  1248.             int startpos, int range,
  1249.             struct re_registers *regs,
  1250.             int stop);
  1251. extern int re_search (struct re_pattern_buffer * rxb, __const__ char *string,
  1252.               int size, int startpos, int range,
  1253.               struct re_registers *regs);
  1254. extern int re_match_2 (struct re_pattern_buffer * rxb,
  1255.                __const__ char * string1, int size1,
  1256.                __const__ char * string2, int size2,
  1257.                int pos, struct re_registers *regs, int stop);
  1258. extern int re_match (struct re_pattern_buffer * rxb,
  1259.              __const__ char * string,
  1260.              int size, int pos,
  1261.              struct re_registers *regs);
  1262. extern reg_syntax_t re_set_syntax (reg_syntax_t syntax);
  1263. extern void re_set_registers (struct re_pattern_buffer *bufp,
  1264.                   struct re_registers *regs,
  1265.                   unsigned num_regs,
  1266.                   regoff_t * starts, regoff_t * ends);
  1267. extern __const__ char * re_compile_pattern (__const__ char *pattern,
  1268.                     int length,
  1269.                     struct re_pattern_buffer * rxb);
  1270. extern int re_compile_fastmap (struct re_pattern_buffer * rxb);
  1271. extern char * re_comp (__const__ char *s);
  1272. extern int re_exec (__const__ char *s);
  1273. extern int regcomp (regex_t * preg, __const__ char * pattern, int cflags);
  1274. extern int regexec (__const__ regex_t *preg, __const__ char *string,
  1275.             size_t nmatch, regmatch_t pmatch[],
  1276.             int eflags);
  1277. extern size_t regerror (int errcode, __const__ regex_t *preg,
  1278.             char *errbuf, size_t errbuf_size);
  1279. extern void regfree (regex_t *preg);
  1280.  
  1281. #else /* STDC */
  1282. extern int re_search_2 ();
  1283. extern int re_search ();
  1284. extern int re_match_2 ();
  1285. extern int re_match ();
  1286. extern reg_syntax_t re_set_syntax ();
  1287. extern void re_set_registers ();
  1288. extern __const__ char * re_compile_pattern ();
  1289. extern int re_compile_fastmap ();
  1290. extern char * re_comp ();
  1291. extern int re_exec ();
  1292. extern int regcomp ();
  1293. extern int regexec ();
  1294. extern size_t regerror ();
  1295. extern void regfree ();
  1296.  
  1297. #endif /* STDC */
  1298.  
  1299.  
  1300.  
  1301. #ifdef RX_WANT_RX_DEFS
  1302.  
  1303. struct rx_counter_frame
  1304. {
  1305.   int tag;
  1306.   int val;
  1307.   struct rx_counter_frame * inherited_from; /* If this is a copy. */
  1308.   struct rx_counter_frame * cdr;
  1309. };
  1310.  
  1311. struct rx_backtrack_frame
  1312. {
  1313.   char * counter_stack_sp;
  1314.  
  1315.   /* A frame is used to save the matchers state when it crosses a 
  1316.    * backtracking point.  The `stk_' fields correspond to variables
  1317.    * in re_search_2 (just strip off thes `stk_').  They are documented
  1318.    * tere.
  1319.    */
  1320.   struct rx_superstate * stk_super;
  1321.   unsigned int stk_c;
  1322.   struct rx_string_position stk_test_pos;
  1323.   int stk_last_l;
  1324.   int stk_last_r;
  1325.   int stk_test_ret;
  1326.  
  1327.   /* This is the list of options left to explore at the backtrack
  1328.    * point for which this frame was created. 
  1329.    */
  1330.   struct rx_distinct_future * df;
  1331.   struct rx_distinct_future * first_df;
  1332.  
  1333. #ifdef RX_DEBUG
  1334.    int stk_line_no;
  1335. #endif
  1336. };
  1337.  
  1338. struct rx_stack_chunk
  1339. {
  1340.   struct rx_stack_chunk * next_chunk;
  1341.   int bytes_left;
  1342.   char * sp;
  1343. };
  1344.  
  1345. enum rx_outer_entry
  1346. {
  1347.   rx_outer_start,
  1348.   rx_outer_fastmap,
  1349.   rx_outer_test,
  1350.   rx_outer_restore_pos
  1351. };
  1352.  
  1353. enum rx_fastmap_return
  1354. {
  1355.   rx_fastmap_continuation,
  1356.   rx_fastmap_error,
  1357.   rx_fastmap_ok,
  1358.   rx_fastmap_fail
  1359. };
  1360.  
  1361. enum rx_fastmap_entry
  1362. {
  1363.   rx_fastmap_start,
  1364.   rx_fastmap_string_break
  1365. };
  1366.  
  1367. enum rx_test_return
  1368. {
  1369.   rx_test_continuation,
  1370.   rx_test_error,
  1371.   rx_test_fail,
  1372.   rx_test_ok
  1373. };
  1374.  
  1375. enum rx_test_internal_return
  1376. {
  1377.   rx_test_internal_error,
  1378.   rx_test_found_first,
  1379.   rx_test_line_finished
  1380. };
  1381.  
  1382. enum rx_test_match_entry
  1383. {
  1384.   rx_test_start,
  1385.   rx_test_cache_hit_loop,
  1386.   rx_test_backreference_check,
  1387.   rx_test_backtrack_return
  1388. };
  1389.  
  1390. struct rx_search_state
  1391. {
  1392.   /* Two groups of registers are kept.  The group with the register state
  1393.    * of the current test match, and the group that holds the state at the end
  1394.    * of the best known match, if any.
  1395.    *
  1396.    * For some patterns, there may also be registers saved on the stack.
  1397.    */
  1398.   unsigned num_regs;        /* Includes an element for register zero. */
  1399.   regoff_t * lparen;        /* scratch space for register returns */
  1400.   regoff_t * rparen;
  1401.   regoff_t * best_lpspace;    /* in case the user doesn't want these */
  1402.   regoff_t * best_rpspace;    /* values, we still need space to store
  1403.                  * them.  Normally, this memoryis unused
  1404.                  * and the space pointed to by REGS is 
  1405.                  * used instead.
  1406.                  */
  1407.   
  1408.   int last_l;            /* Highest index of a valid lparen. */
  1409.   int last_r;            /* It's dual. */
  1410.   
  1411.   int * best_lparen;        /* This contains the best known register */
  1412.   int * best_rparen;        /* assignments. 
  1413.                  * This may point to the same mem as
  1414.                  * best_lpspace, or it might point to memory
  1415.                  * passed by the caller.
  1416.                  */
  1417.   int best_last_l;        /* best_last_l:best_lparen::last_l:lparen */
  1418.   int best_last_r;
  1419.  
  1420.  
  1421.   unsigned char * translate;  
  1422.  
  1423.   struct rx_string_position outer_pos;
  1424.  
  1425.   struct rx_superstate * start_super;
  1426.   int nfa_choice;
  1427.   int first_found;        /* If true, return after finding any match. */
  1428.   int ret_val;
  1429.  
  1430.   /* For continuations... */
  1431.   enum rx_outer_entry outer_search_resume_pt;
  1432.   struct re_pattern_buffer * saved_rxb;
  1433.   int saved_startpos;
  1434.   int saved_range;
  1435.   int saved_stop;
  1436.   int saved_total_size;
  1437.   rx_get_burst_fn saved_get_burst;
  1438.   rx_back_check_fn saved_back_check;
  1439.   struct re_registers * saved_regs;
  1440.   
  1441.   /**
  1442.    ** state for fastmap
  1443.    **/
  1444.   char * fastmap;
  1445.   int fastmap_chr;
  1446.   int fastmap_val;
  1447.  
  1448.   /* for continuations in the fastmap procedure: */
  1449.   enum rx_fastmap_entry fastmap_resume_pt;
  1450.  
  1451.   /**
  1452.    ** state for test_match 
  1453.    **/
  1454.  
  1455.   /* The current superNFA position of the matcher. */
  1456.   struct rx_superstate * super;
  1457.   
  1458.   /* The matcher interprets a series of instruction frames.
  1459.    * This is the `instruction counter' for the interpretation.
  1460.    */
  1461.   struct rx_inx * ifr;
  1462.   
  1463.   /* We insert a ghost character in the string to prime
  1464.    * the nfa.  test_pos.pos, test_pos.str_half, and test_pos.end_half
  1465.    * keep track of the test-match position and string-half.
  1466.    */
  1467.   unsigned char c;
  1468.   
  1469.   /* Position within the string. */
  1470.   struct rx_string_position test_pos;
  1471.  
  1472.   struct rx_stack_chunk * counter_stack;
  1473.   struct rx_stack_chunk * backtrack_stack;
  1474.   int backtrack_frame_bytes;
  1475.   int chunk_bytes;
  1476.   struct rx_stack_chunk * free_chunks;
  1477.  
  1478.   /* To return from this function, set test_ret and 
  1479.    * `goto test_do_return'.
  1480.    *
  1481.    * Possible return values are:
  1482.    *     1   --- end of string while the superNFA is still going
  1483.    *     0   --- internal error (out of memory)
  1484.    *    -1   --- search completed by reaching the superNFA fail state
  1485.    *    -2   --- a match was found, maybe not the longest.
  1486.    *
  1487.    * When the search is complete (-1), best_last_r indicates whether
  1488.    * a match was found.
  1489.    *
  1490.    * -2 is return only if search_state.first_found is non-zero.
  1491.    *
  1492.    * if search_state.first_found is non-zero, a return of -1 indicates no match,
  1493.    * otherwise, best_last_r has to be checked.
  1494.    */
  1495.   int test_ret;
  1496.  
  1497.   int could_have_continued;
  1498.   
  1499. #ifdef RX_DEBUG
  1500.   int backtrack_depth;
  1501.   /* There is a search tree with every node as set of deterministic
  1502.    * transitions in the super nfa.  For every branch of a 
  1503.    * backtrack point is an edge in the tree.
  1504.    * This counts up a pre-order of nodes in that tree.
  1505.    * It's saved on the search stack and printed when debugging. 
  1506.    */
  1507.   int line_no;
  1508.   int lines_found;
  1509. #endif
  1510.  
  1511.  
  1512.   /* For continuations within the match tester */
  1513.   enum rx_test_match_entry test_match_resume_pt;
  1514.   struct rx_inx * saved_next_tr_table;
  1515.   struct rx_inx * saved_this_tr_table;
  1516.   int saved_reg;
  1517.   struct rx_backtrack_frame * saved_bf;
  1518.   
  1519. };
  1520.  
  1521.  
  1522. extern char rx_slowmap[];
  1523. extern unsigned char rx_id_translation[];
  1524.  
  1525. static __inline__ void
  1526. init_fastmap (rxb, search_state)
  1527.      struct re_pattern_buffer * rxb;
  1528.      struct rx_search_state * search_state;
  1529. {
  1530.   search_state->fastmap = (rxb->fastmap
  1531.                ? (char *)rxb->fastmap
  1532.                : (char *)rx_slowmap);
  1533.   /* Update the fastmap now if not correct already. 
  1534.    * When the regexp was compiled, the fastmap was computed
  1535.    * and stored in a bitset.  This expands the bitset into a
  1536.    * character array containing 1s and 0s.
  1537.    */
  1538.   if ((search_state->fastmap == rxb->fastmap) && !rxb->fastmap_accurate)
  1539.     rx_blow_up_fastmap (rxb);
  1540.   search_state->fastmap_chr = -1;
  1541.   search_state->fastmap_val = 0;
  1542.   search_state->fastmap_resume_pt = rx_fastmap_start;
  1543. }
  1544.  
  1545. static __inline__ void
  1546. uninit_fastmap (rxb, search_state)
  1547.      struct re_pattern_buffer * rxb;
  1548.      struct rx_search_state * search_state;
  1549. {
  1550.   /* Unset the fastmap sentinel */
  1551.   if (search_state->fastmap_chr >= 0)
  1552.     search_state->fastmap[search_state->fastmap_chr]
  1553.       = search_state->fastmap_val;
  1554. }
  1555.  
  1556. static __inline__ int
  1557. fastmap_search (rxb, stop, get_burst, app_closure, search_state)
  1558.      struct re_pattern_buffer * rxb;
  1559.      int stop;
  1560.      rx_get_burst_fn get_burst;
  1561.      void * app_closure;
  1562.      struct rx_search_state * search_state;
  1563. {
  1564.   enum rx_fastmap_entry pc;
  1565.  
  1566.   if (0)
  1567.     {
  1568.     return_continuation:
  1569.       search_state->fastmap_resume_pt = pc;
  1570.       return rx_fastmap_continuation;
  1571.     }
  1572.  
  1573.   pc = search_state->fastmap_resume_pt;
  1574.  
  1575.   switch (pc)
  1576.     {
  1577.     case rx_fastmap_start:
  1578.     init_fastmap_sentinal:
  1579.       /* For the sake of fast fastmapping, set a sentinal in the fastmap.
  1580.        * This sentinal will trap the fastmap loop when it reaches the last
  1581.        * valid character in a string half.
  1582.        *
  1583.        * This must be reset when the fastmap/search loop crosses a string 
  1584.        * boundry, and before returning to the caller.  So sometimes,
  1585.        * the fastmap loop is restarted with `continue', othertimes by
  1586.        * `goto init_fastmap_sentinal'.
  1587.        */
  1588.       if (search_state->outer_pos.size)
  1589.     {
  1590.       search_state->fastmap_chr = ((search_state->outer_pos.search_direction == 1)
  1591.                        ? *(search_state->outer_pos.end - 1)
  1592.                        : *search_state->outer_pos.string);
  1593.       search_state->fastmap_val
  1594.         = search_state->fastmap[search_state->fastmap_chr];
  1595.       search_state->fastmap[search_state->fastmap_chr] = 1;
  1596.     }
  1597.       else
  1598.     {
  1599.       search_state->fastmap_chr = -1;
  1600.       search_state->fastmap_val = 0;
  1601.     }
  1602.       
  1603.       if (search_state->outer_pos.pos >= search_state->outer_pos.end)
  1604.     goto fastmap_hit_bound;
  1605.       else
  1606.     {
  1607.       if (search_state->outer_pos.search_direction == 1)
  1608.         {
  1609.           if (search_state->fastmap_val)
  1610.         {
  1611.           for (;;)
  1612.             {
  1613.               while (!search_state->fastmap[*search_state->outer_pos.pos])
  1614.             ++search_state->outer_pos.pos;
  1615.               return rx_fastmap_ok;
  1616.             }
  1617.         }
  1618.           else
  1619.         {
  1620.           for (;;)
  1621.             {
  1622.               while (!search_state->fastmap[*search_state->outer_pos.pos])
  1623.             ++search_state->outer_pos.pos;
  1624.               if (*search_state->outer_pos.pos != search_state->fastmap_chr)
  1625.             return rx_fastmap_ok;
  1626.               else 
  1627.             {
  1628.               ++search_state->outer_pos.pos;
  1629.               if (search_state->outer_pos.pos == search_state->outer_pos.end)
  1630.                 goto fastmap_hit_bound;
  1631.             }
  1632.             }
  1633.         }
  1634.         }
  1635.       else
  1636.         {
  1637.           __const__ unsigned char * bound;
  1638.           bound = search_state->outer_pos.string - 1;
  1639.           if (search_state->fastmap_val)
  1640.         {
  1641.           for (;;)
  1642.             {
  1643.               while (!search_state->fastmap[*search_state->outer_pos.pos])
  1644.             --search_state->outer_pos.pos;
  1645.               return rx_fastmap_ok;
  1646.             }
  1647.         }
  1648.           else
  1649.         {
  1650.           for (;;)
  1651.             {
  1652.               while (!search_state->fastmap[*search_state->outer_pos.pos])
  1653.             --search_state->outer_pos.pos;
  1654.               if ((*search_state->outer_pos.pos != search_state->fastmap_chr) || search_state->fastmap_val)
  1655.             return rx_fastmap_ok;
  1656.               else 
  1657.             {
  1658.               --search_state->outer_pos.pos;
  1659.               if (search_state->outer_pos.pos == bound)
  1660.                 goto fastmap_hit_bound;
  1661.             }
  1662.             }
  1663.         }
  1664.         }
  1665.     }
  1666.       
  1667.     case rx_fastmap_string_break:
  1668.     fastmap_hit_bound:
  1669.       {
  1670.     /* If we hit a bound, it may be time to fetch another burst
  1671.      * of string, or it may be time to return a continuation to 
  1672.       * the caller, or it might be time to fail.
  1673.      */
  1674.  
  1675.     int burst_state;
  1676.     burst_state = get_burst (&search_state->outer_pos, app_closure, stop);
  1677.     switch (burst_state)
  1678.       {
  1679.       case rx_get_burst_continuation:
  1680.         {
  1681.           pc = rx_fastmap_string_break;
  1682.           goto return_continuation;
  1683.         }
  1684.       case rx_get_burst_error:
  1685.         return rx_fastmap_error;
  1686.       case rx_get_burst_ok:
  1687.         goto init_fastmap_sentinal;
  1688.       case rx_get_burst_no_more:
  1689.         /* ...not a string split, simply no more string. 
  1690.          *
  1691.          * When searching backward, running out of string
  1692.          * is reason to quit.
  1693.          *
  1694.          * When searching forward, we allow the possibility
  1695.          * of an (empty) match after the last character in the
  1696.          * virtual string.  So, fall through to the matcher
  1697.          */
  1698.         return (  (search_state->outer_pos.search_direction == 1)
  1699.             ? rx_fastmap_ok
  1700.             : rx_fastmap_fail);
  1701.       }
  1702.       }
  1703.     }
  1704.  
  1705. }
  1706.  
  1707.  
  1708.  
  1709. #ifdef emacs
  1710. /* The `emacs' switch turns on certain matching commands
  1711.  * that make sense only in Emacs. 
  1712.  */
  1713. #include "config.h"
  1714. #include "lisp.h"
  1715. #include "buffer.h"
  1716. #include "syntax.h"
  1717. /* Emacs uses `NULL' as a predicate.  */
  1718. #undef NULL
  1719. #else  /* not emacs */
  1720. /* Setting RX_MEMDBUG is useful if you have dbmalloc.  Maybe with similar
  1721.  * packages too.
  1722.  */
  1723. #ifdef RX_MEMDBUG
  1724. #include <malloc.h>
  1725. #else /* not RX_RX_MEMDBUG */
  1726.  
  1727. /* We used to test for `BSTRING' here, but only GCC and Emacs define
  1728.  * `BSTRING', as far as I know, and neither of them use this code.  
  1729.  */
  1730. #if HAVE_STRING_H || STDC_HEADERS
  1731. #include <string.h>
  1732.  
  1733. #ifndef bcmp
  1734. #define bcmp(s1, s2, n)    memcmp ((s1), (s2), (n))
  1735. #endif
  1736.  
  1737. #ifndef bcopy
  1738. #define bcopy(s, d, n)    memcpy ((d), (s), (n))
  1739. #endif
  1740.  
  1741. #ifndef bzero
  1742. #define bzero(s, n)    memset ((s), 0, (n))
  1743. #endif
  1744.  
  1745. #else /*  HAVE_STRING_H || STDC_HEADERS */
  1746. #include <strings.h>
  1747. #endif   /* not RX_MEMDBUG */
  1748.  
  1749. #ifdef STDC_HEADERS
  1750. #include <stdlib.h>
  1751. #else /* not STDC_HEADERS */
  1752. char *malloc ();
  1753. char *realloc ();
  1754. #endif /* not STDC_HEADERS */
  1755.  
  1756. #endif /* not emacs */
  1757.  
  1758.  
  1759.  
  1760. /* Define the syntax basics for \<, \>, etc.
  1761.  * This must be nonzero for the wordchar and notwordchar pattern
  1762.  * commands in re_match_2.
  1763.  */
  1764. #ifndef Sword 
  1765. #define Sword 1
  1766. #endif
  1767.  
  1768. /* How many characters in the character set.  */
  1769. #define CHAR_SET_SIZE (1 << CHARBITS)
  1770. #define SYNTAX(c) re_syntax_table[c]
  1771. RX_DECL char re_syntax_table[CHAR_SET_SIZE];
  1772.  
  1773. #endif /* not emacs */
  1774.  
  1775.  
  1776. /* Test if at very beginning or at very end of the virtual concatenation
  1777.  *  of `string1' and `string2'.  If only one string, it's `string2'.  
  1778.  */
  1779.  
  1780. #define AT_STRINGS_BEG() \
  1781.   (   -1         \
  1782.    == ((search_state.test_pos.pos - search_state.test_pos.string) \
  1783.        + search_state.test_pos.offset))
  1784.  
  1785. #define AT_STRINGS_END() \
  1786.   (   (total_size - 1)     \
  1787.    == ((search_state.test_pos.pos - search_state.test_pos.string) \
  1788.        + search_state.test_pos.offset))
  1789.  
  1790.  
  1791. /* Test if POS + 1 points to a character which is word-constituent.  We have
  1792.  * two special cases to check for: if past the end of string1, look at
  1793.  * the first character in string2; and if before the beginning of
  1794.  * string2, look at the last character in string1.
  1795.  *
  1796.  * Assumes `string1' exists, so use in conjunction with AT_STRINGS_BEG ().  
  1797.  */
  1798. #define LETTER_P(POS,OFF)                        \
  1799.   (   SYNTAX (fetch_char(POS, OFF, app_closure, stop))            \
  1800.    == Sword)
  1801.  
  1802. /* Test if the character at D and the one after D differ with respect
  1803.  * to being word-constituent.  
  1804.  */
  1805. #define AT_WORD_BOUNDARY(d)                        \
  1806.   (AT_STRINGS_BEG () || AT_STRINGS_END () || LETTER_P (d,0) != LETTER_P (d, 1))
  1807.  
  1808.  
  1809. #ifdef RX_SUPPORT_CONTINUATIONS
  1810. #define RX_STACK_ALLOC(BYTES) malloc(BYTES)
  1811. #define RX_STACK_FREE(MEM) free(MEM)
  1812. #else
  1813. #define RX_STACK_ALLOC(BYTES) alloca(BYTES)
  1814. #define RX_STACK_FREE(MEM) \
  1815.       ((struct rx_stack_chunk *)MEM)->next_chunk = search_state.free_chunks; \
  1816.       search_state.free_chunks = ((struct rx_stack_chunk *)MEM);
  1817.  
  1818. #endif
  1819.  
  1820. #define PUSH(CHUNK_VAR,BYTES)   \
  1821.   if (!CHUNK_VAR || (CHUNK_VAR->bytes_left < (BYTES)))  \
  1822.     {                    \
  1823.       struct rx_stack_chunk * new_chunk;    \
  1824.       if (search_state.free_chunks)            \
  1825.     {                \
  1826.       new_chunk = search_state.free_chunks;    \
  1827.       search_state.free_chunks = search_state.free_chunks->next_chunk; \
  1828.     }                \
  1829.       else                \
  1830.     {                \
  1831.       new_chunk = (struct rx_stack_chunk *)RX_STACK_ALLOC(search_state.chunk_bytes); \
  1832.       if (!new_chunk)        \
  1833.         {                \
  1834.           search_state.ret_val = 0;        \
  1835.           goto test_do_return;    \
  1836.         }                \
  1837.     }                \
  1838.       new_chunk->sp = (char *)new_chunk + sizeof (struct rx_stack_chunk); \
  1839.       new_chunk->bytes_left = (search_state.chunk_bytes \
  1840.                    - (BYTES) \
  1841.                    - sizeof (struct rx_stack_chunk)); \
  1842.       new_chunk->next_chunk = CHUNK_VAR; \
  1843.       CHUNK_VAR = new_chunk;        \
  1844.     } \
  1845.   else \
  1846.     (CHUNK_VAR->sp += (BYTES)), (CHUNK_VAR->bytes_left -= (BYTES))
  1847.  
  1848. #define POP(CHUNK_VAR,BYTES) \
  1849.   if (CHUNK_VAR->sp == ((char *)CHUNK_VAR + sizeof(*CHUNK_VAR))) \
  1850.     { \
  1851.       struct rx_stack_chunk * new_chunk = CHUNK_VAR->next_chunk; \
  1852.       RX_STACK_FREE(CHUNK_VAR); \
  1853.       CHUNK_VAR = new_chunk; \
  1854.     } \
  1855.   else \
  1856.     (CHUNK_VAR->sp -= BYTES), (CHUNK_VAR->bytes_left += BYTES)
  1857.  
  1858.  
  1859.  
  1860. #define SRCH_TRANSLATE(C)  search_state.translate[(unsigned char) (C)]
  1861.  
  1862.  
  1863.  
  1864.  
  1865. #ifdef __STDC__
  1866. RX_DECL __inline__ int
  1867. rx_search  (struct re_pattern_buffer * rxb,
  1868.         int startpos,
  1869.         int range,
  1870.         int stop,
  1871.         int total_size,
  1872.         rx_get_burst_fn get_burst,
  1873.         rx_back_check_fn back_check,
  1874.         rx_fetch_char_fn fetch_char,
  1875.         void * app_closure,
  1876.         struct re_registers * regs,
  1877.         struct rx_search_state * resume_state,
  1878.         struct rx_search_state * save_state)
  1879. #else
  1880. RX_DECL __inline__ int
  1881. rx_search  (rxb, startpos, range, stop, total_size,
  1882.         get_burst, back_check, fetch_char,
  1883.         app_closure, regs, resume_state, save_state)
  1884.      struct re_pattern_buffer * rxb;
  1885.      int startpos;
  1886.      int range;
  1887.      int stop;
  1888.      int total_size;
  1889.      rx_get_burst_fn get_burst;
  1890.      rx_back_check_fn back_check;
  1891.      rx_fetch_char_fn fetch_char;
  1892.      void * app_closure;
  1893.      struct re_registers * regs;
  1894.      struct rx_search_state * resume_state;
  1895.      struct rx_search_state * save_state;
  1896. #endif
  1897. {
  1898.   int pc;
  1899.   int test_state;
  1900.   struct rx_search_state search_state;
  1901.  
  1902.   if (!resume_state)
  1903.     pc = rx_outer_start;
  1904.   else
  1905.     {
  1906.       search_state = *resume_state;
  1907.       regs = search_state.saved_regs;
  1908.       rxb = search_state.saved_rxb;
  1909.       startpos = search_state.saved_startpos;
  1910.       range = search_state.saved_range;
  1911.       stop = search_state.saved_stop;
  1912.       total_size = search_state.saved_total_size;
  1913.       get_burst = search_state.saved_get_burst;
  1914.       back_check = search_state.saved_back_check;
  1915.       pc = search_state.outer_search_resume_pt;
  1916.       if (0)
  1917.     {
  1918.     return_continuation:
  1919.       if (save_state)
  1920.         {
  1921.           *save_state = search_state;
  1922.           save_state->saved_regs = regs;
  1923.           save_state->saved_rxb = rxb;
  1924.           save_state->saved_startpos = startpos;
  1925.           save_state->saved_range = range;
  1926.           save_state->saved_stop = stop;
  1927.           save_state->saved_total_size = total_size;
  1928.           save_state->saved_get_burst = get_burst;
  1929.           save_state->saved_back_check = back_check;
  1930.           save_state->outer_search_resume_pt = pc;
  1931.         }
  1932.       return rx_search_continuation;
  1933.     }
  1934.     }
  1935.  
  1936.   switch (pc)
  1937.     {
  1938.     case rx_outer_start:
  1939.       search_state.ret_val = rx_search_fail;
  1940.       (  search_state.lparen
  1941.        = search_state.rparen
  1942.        = search_state.best_lpspace
  1943.        = search_state.best_rpspace
  1944.        = 0);
  1945.       
  1946.       /* figure the number of registers we may need for use in backreferences.
  1947.        * the number here includes an element for register zero.  
  1948.        */
  1949.       search_state.num_regs = rxb->re_nsub + 1;
  1950.       
  1951.       
  1952.       /* check for out-of-range startpos.  */
  1953.       if ((startpos < 0) || (startpos > total_size))
  1954.     return rx_search_fail;
  1955.       
  1956.       /* fix up range if it might eventually take us outside the string. */
  1957.       {
  1958.     int endpos;
  1959.     endpos = startpos + range;
  1960.     if (endpos < -1)
  1961.       range = (-1 - startpos);
  1962.     else if (endpos > total_size)
  1963.       range = total_size - startpos;
  1964.       }
  1965.       
  1966.       /* if the search isn't to be a backwards one, don't waste time in a
  1967.        * long search for a pattern that says it is anchored.
  1968.        */
  1969.       if (rxb->begbuf_only && (range > 0))
  1970.     {
  1971.       if (startpos > 0)
  1972.         return rx_search_fail;
  1973.       else
  1974.         range = 1;
  1975.     }
  1976.       
  1977.       /* decide whether to use internal or user-provided reg buffers. */
  1978.       if (!regs || rxb->no_sub)
  1979.     {
  1980.       search_state.best_lpspace =
  1981.         (regoff_t *)REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
  1982.       search_state.best_rpspace =
  1983.         (regoff_t *)REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
  1984.       search_state.best_lparen = search_state.best_lpspace;
  1985.       search_state.best_rparen = search_state.best_rpspace;
  1986.     }
  1987.       else
  1988.     {    
  1989.       /* have the register data arrays been allocated?  */
  1990.       if (rxb->regs_allocated == REGS_UNALLOCATED)
  1991.         { /* no.  so allocate them with malloc.  we need one
  1992.          extra element beyond `search_state.num_regs' for the `-1' marker
  1993.          gnu code uses.  */
  1994.           regs->num_regs = MAX (RE_NREGS, rxb->re_nsub + 1);
  1995.           regs->start = ((regoff_t *)
  1996.                  malloc (regs->num_regs * sizeof ( regoff_t)));
  1997.           regs->end = ((regoff_t *)
  1998.                malloc (regs->num_regs * sizeof ( regoff_t)));
  1999.           if (regs->start == 0 || regs->end == 0)
  2000.         return rx_search_error;
  2001.           rxb->regs_allocated = REGS_REALLOCATE;
  2002.         }
  2003.       else if (rxb->regs_allocated == REGS_REALLOCATE)
  2004.         { /* yes.  if we need more elements than were already
  2005.          allocated, reallocate them.  if we need fewer, just
  2006.          leave it alone.  */
  2007.           if (regs->num_regs < search_state.num_regs + 1)
  2008.         {
  2009.           regs->num_regs = search_state.num_regs + 1;
  2010.           regs->start = ((regoff_t *)
  2011.                  realloc (regs->start,
  2012.                       regs->num_regs * sizeof (regoff_t)));
  2013.           regs->end = ((regoff_t *)
  2014.                    realloc (regs->end,
  2015.                     regs->num_regs * sizeof ( regoff_t)));
  2016.           if (regs->start == 0 || regs->end == 0)
  2017.             return rx_search_error;
  2018.         }
  2019.         }
  2020.       else if (rxb->regs_allocated != REGS_FIXED)
  2021.         return rx_search_error;
  2022.       
  2023.       if (regs->num_regs < search_state.num_regs + 1)
  2024.         {
  2025.           search_state.best_lpspace =
  2026.         ((regoff_t *)
  2027.          REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t)));
  2028.           search_state.best_rpspace =
  2029.         ((regoff_t *)
  2030.          REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t)));
  2031.           search_state.best_lparen = search_state.best_lpspace;
  2032.           search_state.best_rparen = search_state.best_rpspace;
  2033.         }
  2034.       else
  2035.         {
  2036.           search_state.best_lparen = regs->start;
  2037.           search_state.best_rparen = regs->end;
  2038.         }
  2039.     }
  2040.       
  2041.       search_state.lparen =
  2042.     (regoff_t *) REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
  2043.       search_state.rparen =
  2044.     (regoff_t *) REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t)); 
  2045.       
  2046.       if (! (   search_state.best_rparen
  2047.          && search_state.best_lparen
  2048.          && search_state.lparen && search_state.rparen))
  2049.     return rx_search_error;
  2050.       
  2051.       search_state.best_last_l = search_state.best_last_r = -1;
  2052.       
  2053.       search_state.translate = (rxb->translate
  2054.                 ? rxb->translate
  2055.                 : rx_id_translation);
  2056.       
  2057.       
  2058.       
  2059.       /*
  2060.        * two nfa's were compiled.  
  2061.        * `0' is complete.
  2062.        * `1' faster but gets registers wrong and ends too soon.
  2063.        */
  2064.       search_state.nfa_choice = (regs && !rxb->least_subs) ? '\0' : '\1';
  2065.       
  2066.       /* we have the option to look for the best match or the first
  2067.        * one we can find.  if the user isn't asking for register information,
  2068.        * we don't need to find the best match.
  2069.        */
  2070.       search_state.first_found = !regs;
  2071.       
  2072.       if (range >= 0)
  2073.     {
  2074.       search_state.outer_pos.search_end = MIN (total_size, startpos + range) + 1;
  2075.       search_state.outer_pos.search_direction = 1;
  2076.     }
  2077.       else
  2078.     {
  2079.       search_state.outer_pos.search_end = MAX(-1, startpos + range);
  2080.       search_state.outer_pos.search_direction = -1;
  2081.     }
  2082.       
  2083.       /* the vacuous search always turns up nothing. */
  2084.       if ((search_state.outer_pos.search_direction == 1)
  2085.       ? (startpos > search_state.outer_pos.search_end)
  2086.       : (startpos < search_state.outer_pos.search_end))
  2087.     return rx_search_fail;
  2088.       
  2089.       /* now we build the starting state of the supernfa. */
  2090.       {
  2091.     struct rx_superset * start_contents;
  2092.     struct rx_nfa_state_set * start_nfa_set;
  2093.     
  2094.     /* we presume here that the nfa start state has only one
  2095.      * possible future with no side effects.  
  2096.      */
  2097.     start_nfa_set = rxb->start->futures->destset;
  2098.     if (   rxb->rx.start_set
  2099.         && (rxb->rx.start_set->starts_for == &rxb->rx))
  2100.       start_contents = rxb->rx.start_set;
  2101.     else
  2102.       {
  2103.         start_contents =
  2104.           rx_superstate_eclosure_union (&rxb->rx,
  2105.                         rx_superset_cons (&rxb->rx, 0, 0),
  2106.                         start_nfa_set);
  2107.         
  2108.         if (!start_contents)
  2109.           return rx_search_fail;
  2110.         
  2111.         start_contents->starts_for = &rxb->rx;
  2112.         rxb->rx.start_set = start_contents;
  2113.       }
  2114.     if (   start_contents->superstate
  2115.         && (start_contents->superstate->rx_id == rxb->rx.rx_id))
  2116.       {
  2117.         search_state.start_super = start_contents->superstate;
  2118.         rx_lock_superstate (&rxb->rx, search_state.start_super);
  2119.       }
  2120.     else
  2121.       {
  2122.         rx_protect_superset (&rxb->rx, start_contents);
  2123.         
  2124.         search_state.start_super = rx_superstate (&rxb->rx, start_contents);
  2125.         if (!search_state.start_super)
  2126.           return rx_search_fail;
  2127.         rx_lock_superstate (&rxb->rx, search_state.start_super);
  2128.         rx_release_superset (&rxb->rx, start_contents);
  2129.       }
  2130.       }
  2131.       
  2132.       
  2133.     
  2134.       (  search_state.outer_pos.string
  2135.        = search_state.outer_pos.end
  2136.        = 0);
  2137.  
  2138.       search_state.outer_pos.offset = 0;
  2139.       search_state.outer_pos.size = 0;
  2140.       search_state.outer_pos.pos = (unsigned char *)startpos;
  2141.       init_fastmap (rxb, &search_state);
  2142.  
  2143.       search_state.fastmap_resume_pt = rx_fastmap_start;
  2144.     case rx_outer_fastmap:
  2145.       /* do { */
  2146.     pseudo_do:
  2147.       {
  2148.     {
  2149.       int fastmap_state;
  2150.       fastmap_state = fastmap_search (rxb, stop, get_burst, app_closure,
  2151.                       &search_state);
  2152.       switch (fastmap_state)
  2153.         {
  2154.         case rx_fastmap_continuation:
  2155.           pc = rx_outer_fastmap;
  2156.           goto return_continuation;
  2157.         case rx_fastmap_fail:
  2158.           goto finish;
  2159.         case rx_fastmap_ok:
  2160.           break;
  2161.         }
  2162.     }
  2163.     
  2164.     /* now the fastmap loop has brought us to a plausible 
  2165.      * starting point for a match.  so, it's time to run the
  2166.      * nfa and see if a match occured.
  2167.      */
  2168.     startpos = (  search_state.outer_pos.pos
  2169.             - search_state.outer_pos.string
  2170.             + search_state.outer_pos.offset);
  2171.     if (startpos == search_state.outer_pos.search_end)
  2172.       goto finish;
  2173.       }
  2174.  
  2175.       search_state.test_match_resume_pt = rx_test_start;
  2176.       /* do interrupted for entry point... */
  2177.     case rx_outer_test:
  2178.       /* ...do continued */
  2179.       {
  2180.     goto test_match;
  2181.       test_returns_to_search:
  2182.     switch (test_state)
  2183.       {
  2184.       case rx_test_continuation:
  2185.         pc = rx_outer_test;
  2186.         goto return_continuation;
  2187.       case rx_test_error:
  2188.         search_state.ret_val = rx_search_error;
  2189.         goto finish;
  2190.       case rx_test_fail:
  2191.         break;
  2192.       case rx_test_ok:
  2193.         goto finish;
  2194.       }
  2195.     search_state.outer_pos.pos += search_state.outer_pos.search_direction;
  2196.     startpos += search_state.outer_pos.search_direction;
  2197.       }
  2198.       /* do interrupted for entry point... */
  2199.     case rx_outer_restore_pos:
  2200.       {
  2201.     int x;
  2202.     x = get_burst (&search_state.outer_pos, app_closure, stop);
  2203.     switch (x)
  2204.       {
  2205.       case rx_get_burst_continuation:
  2206.         pc = rx_outer_restore_pos;
  2207.         goto return_continuation;
  2208.       case rx_get_burst_error:
  2209.         search_state.ret_val = rx_search_error;
  2210.         goto finish;
  2211.       case rx_get_burst_no_more:
  2212.         goto finish;
  2213.       case rx_get_burst_ok:
  2214.         break;
  2215.       }
  2216.       } /* } while (...see below...) */
  2217.       if ((search_state.outer_pos.search_direction == 1)
  2218.       ? (startpos < search_state.outer_pos.search_end)
  2219.       : (startpos > search_state.outer_pos.search_end))
  2220.     goto pseudo_do;
  2221.  
  2222.     
  2223.     finish:
  2224.       uninit_fastmap (rxb, &search_state);
  2225.       if (search_state.start_super)
  2226.     rx_unlock_superstate (&rxb->rx, search_state.start_super);
  2227.       
  2228. #ifdef regex_malloc
  2229.       if (search_state.lparen) free (search_state.lparen);
  2230.       if (search_state.rparen) free (search_state.rparen);
  2231.       if (search_state.best_lpspace) free (search_state.best_lpspace);
  2232.       if (search_state.best_rpspace) free (search_state.best_rpspace);
  2233. #endif
  2234.       return search_state.ret_val;
  2235.     }
  2236.  
  2237.  
  2238.  test_match:
  2239.   {
  2240.     enum rx_test_match_entry test_pc;
  2241.     int inx;
  2242.     test_pc = search_state.test_match_resume_pt;
  2243.     if (test_pc == rx_test_start)
  2244.       {
  2245. #ifdef RX_DEBUG
  2246.     search_state.backtrack_depth = 0;
  2247. #endif
  2248.     search_state.last_l = search_state.last_r = 0;
  2249.     search_state.lparen[0] = startpos;
  2250.     search_state.super = search_state.start_super;
  2251.     search_state.c = search_state.nfa_choice;
  2252.     search_state.test_pos.pos = search_state.outer_pos.pos - 1;    
  2253.     search_state.test_pos.string = search_state.outer_pos.string;
  2254.     search_state.test_pos.end = search_state.outer_pos.end;
  2255.     search_state.test_pos.offset = search_state.outer_pos.offset;
  2256.     search_state.test_pos.size = search_state.outer_pos.size;
  2257.     search_state.test_pos.search_direction = 1;
  2258.     search_state.counter_stack = 0;
  2259.     search_state.backtrack_stack = 0;
  2260.     search_state.backtrack_frame_bytes =
  2261.       (sizeof (struct rx_backtrack_frame)
  2262.        + (rxb->match_regs_on_stack
  2263.           ? sizeof (regoff_t) * (search_state.num_regs + 1) * 2
  2264.           : 0));
  2265.     search_state.chunk_bytes = search_state.backtrack_frame_bytes * 64;
  2266.     search_state.free_chunks = 0;
  2267.     search_state.test_ret = rx_test_line_finished;
  2268.     search_state.could_have_continued = 0;
  2269.       }  
  2270.     /* This is while (1)...except that the body of the loop is interrupted 
  2271.      * by some alternative entry points.
  2272.      */
  2273.   pseudo_while_1:
  2274.     switch (test_pc)
  2275.       {
  2276.       case rx_test_cache_hit_loop:
  2277.     goto resume_continuation_1;
  2278.       case rx_test_backreference_check:
  2279.     goto resume_continuation_2;
  2280.       case rx_test_backtrack_return:
  2281.     goto resume_continuation_3;
  2282.       case rx_test_start:
  2283. #ifdef RX_DEBUG
  2284.     /* There is a search tree with every node as set of deterministic
  2285.      * transitions in the super nfa.  For every branch of a 
  2286.      * backtrack point is an edge in the tree.
  2287.      * This counts up a pre-order of nodes in that tree.
  2288.      * It's saved on the search stack and printed when debugging. 
  2289.      */
  2290.     search_state.line_no = 0;
  2291.     search_state.lines_found = 0;
  2292. #endif
  2293.     
  2294.       top_of_cycle:
  2295.     /* A superstate is basicly a transition table, indexed by 
  2296.      * characters from the string being tested, and containing 
  2297.      * RX_INX (`instruction frame') structures.
  2298.      */
  2299.     search_state.ifr = &search_state.super->transitions [search_state.c];
  2300.     
  2301.       recurse_test_match:
  2302.     /* This is the point to which control is sent when the
  2303.      * test matcher `recurses'.  Before jumping here, some variables
  2304.      * need to be saved on the stack and the next instruction frame
  2305.      * has to be computed.
  2306.      */
  2307.     
  2308.       restart:
  2309.     /* Some instructions don't advance the matcher, but just
  2310.      * carry out some side effects and fetch a new instruction.
  2311.      * To dispatch that new instruction, `goto restart'.
  2312.      */
  2313.     
  2314.     {
  2315.       struct rx_inx * next_tr_table;
  2316.       struct rx_inx * this_tr_table;
  2317.       /* The fastest route through the loop is when the instruction 
  2318.        * is RX_NEXT_CHAR.  This case is detected when SEARCH_STATE.IFR->DATA
  2319.        * is non-zero.  In that case, it points to the next
  2320.        * superstate. 
  2321.        *
  2322.        * This allows us to not bother fetching the bytecode.
  2323.        */
  2324.       next_tr_table = (struct rx_inx *)search_state.ifr->data;
  2325.       this_tr_table = search_state.super->transitions;
  2326.       while (next_tr_table)
  2327.         {
  2328. #ifdef RX_DEBUG
  2329.           if (rx_debug_trace)
  2330.         {
  2331.           struct rx_superset * setp;
  2332.           
  2333.           fprintf (stderr, "%d %d>> re_next_char @ %d (%d)",
  2334.                search_state.line_no,
  2335.                search_state.backtrack_depth,
  2336.                (search_state.test_pos.pos - search_state.test_pos.string
  2337.                 + search_state.test_pos.offset), search_state.c);
  2338.           
  2339.           search_state.super =
  2340.             ((struct rx_superstate *)
  2341.              ((char *)this_tr_table
  2342.               - ((unsigned long)
  2343.              ((struct rx_superstate *)0)->transitions)));
  2344.           
  2345.           setp = search_state.super->contents;
  2346.           fprintf (stderr, "   superstet (rx=%d, &=%x: ",
  2347.                rxb->rx.rx_id, setp);
  2348.           while (setp)
  2349.             {
  2350.               fprintf (stderr, "%d ", setp->id);
  2351.               setp = setp->cdr;
  2352.             }
  2353.           fprintf (stderr, "\n");
  2354.         }
  2355. #endif
  2356.           this_tr_table = next_tr_table;
  2357.           ++search_state.test_pos.pos;
  2358.           if (search_state.test_pos.pos == search_state.test_pos.end)
  2359.         {
  2360.           int burst_state;
  2361.         try_burst_1:
  2362.           burst_state = get_burst (&search_state.test_pos,
  2363.                        app_closure, stop);
  2364.           switch (burst_state)
  2365.             {
  2366.             case rx_get_burst_continuation:
  2367.               search_state.saved_this_tr_table = this_tr_table;
  2368.               search_state.saved_next_tr_table = next_tr_table;
  2369.               test_pc = rx_test_cache_hit_loop;
  2370.               goto test_return_continuation;
  2371.               
  2372.             resume_continuation_1:
  2373.               /* Continuation one jumps here to do its work: */
  2374.               search_state.saved_this_tr_table = this_tr_table;
  2375.               search_state.saved_next_tr_table = next_tr_table;
  2376.               goto try_burst_1;
  2377.               
  2378.             case rx_get_burst_ok:
  2379.               /* get_burst succeeded...keep going */
  2380.               break;
  2381.               
  2382.             case rx_get_burst_no_more:
  2383.               search_state.test_ret = rx_test_line_finished;
  2384.               search_state.could_have_continued = 1;
  2385.               goto test_do_return;
  2386.               
  2387.             case rx_get_burst_error:
  2388.               /* An error... */
  2389.               search_state.test_ret = rx_test_internal_error;
  2390.               goto test_do_return;
  2391.             }
  2392.         }
  2393.           search_state.c = *search_state.test_pos.pos;
  2394.           search_state.ifr = this_tr_table + search_state.c;
  2395.           next_tr_table = (struct rx_inx *)search_state.ifr->data;
  2396.         } /* Fast loop through cached transition tables */
  2397.       
  2398.       /* Here when we ran out of cached next-char transitions. 
  2399.        * So, it will be necessary to do a more expensive
  2400.        * dispatch on the current instruction.  The superstate
  2401.        * pointer is allowed to become invalid during next-char
  2402.        * transitions -- now we must bring it up to date.
  2403.        */
  2404.       search_state.super =
  2405.         ((struct rx_superstate *)
  2406.          ((char *)this_tr_table
  2407.           - ((unsigned long)
  2408.          ((struct rx_superstate *)0)->transitions)));
  2409.     }
  2410.     
  2411.     /* We've encountered an instruction other than next-char.
  2412.      * Dispatch that instruction:
  2413.      */
  2414.     inx = (int)search_state.ifr->inx;
  2415. #ifdef RX_DEBUG
  2416.     if (rx_debug_trace)
  2417.       {
  2418.         struct rx_superset * setp = search_state.super->contents;
  2419.         
  2420.         fprintf (stderr, "%d %d>> %s @ %d (%d)", search_state.line_no,
  2421.              search_state.backtrack_depth,
  2422.              inx_names[inx],
  2423.              (search_state.test_pos.pos - search_state.test_pos.string
  2424.               + (test_pos.half == 0 ? 0 : size1)), search_state.c);
  2425.         
  2426.         fprintf (stderr, "   superstet (rx=%d, &=%x: ",
  2427.              rxb->rx.rx_id, setp);
  2428.         while (setp)
  2429.           {
  2430.         fprintf (stderr, "%d ", setp->id);
  2431.         setp = setp->cdr;
  2432.           }
  2433.         fprintf (stderr, "\n");
  2434.       }
  2435. #endif
  2436.     switch ((enum rx_opcode)inx)
  2437.       {
  2438.       case rx_do_side_effects:
  2439.         
  2440.         /*  RX_DO_SIDE_EFFECTS occurs when we cross epsilon 
  2441.          *  edges associated with parentheses, backreferencing, etc.
  2442.          */
  2443.         {
  2444.           struct rx_distinct_future * df =
  2445.         (struct rx_distinct_future *)search_state.ifr->data_2;
  2446.           struct rx_se_list * el = df->effects;
  2447.           /* Side effects come in lists.  This walks down
  2448.            * a list, dispatching.
  2449.            */
  2450.           while (el)
  2451.         {
  2452.           long effect;
  2453.           effect = (long)el->car;
  2454.           if (effect < 0)
  2455.             {
  2456. #ifdef RX_DEBUG
  2457.               if (rx_debug_trace)
  2458.             {
  2459.               struct rx_superset * setp = search_state.super->contents;
  2460.               
  2461.               fprintf (stderr, "....%d %d>> %s\n", search_state.line_no,
  2462.                    search_state.backtrack_depth,
  2463.                    efnames[-effect]);
  2464.             }
  2465. #endif
  2466.               switch ((enum re_side_effects) effect)
  2467.  
  2468.             {
  2469.             case re_se_pushback:
  2470.               search_state.ifr = &df->future_frame;
  2471.               if (!search_state.ifr->data)
  2472.                 {
  2473.                   struct rx_superstate * sup;
  2474.                   sup = search_state.super;
  2475.                   rx_lock_superstate (rx, sup);
  2476.                   if (!rx_handle_cache_miss (&rxb->rx,
  2477.                              search_state.super,
  2478.                              search_state.c,
  2479.                              (search_state.ifr
  2480.                               ->data_2)))
  2481.                 {
  2482.                   rx_unlock_superstate (rx, sup);
  2483.                   search_state.test_ret = rx_test_internal_error;
  2484.                   goto test_do_return;
  2485.                 }
  2486.                   rx_unlock_superstate (rx, sup);
  2487.                 }
  2488.               /* --search_state.test_pos.pos; */
  2489.               search_state.c = 't';
  2490.               search_state.super
  2491.                 = ((struct rx_superstate *)
  2492.                    ((char *)search_state.ifr->data
  2493.                 - (long)(((struct rx_superstate *)0)
  2494.                      ->transitions)));
  2495.               goto top_of_cycle;
  2496.               break;
  2497.             case re_se_push0:
  2498.               {
  2499.                 struct rx_counter_frame * old_cf
  2500.                   = (search_state.counter_stack
  2501.                  ? ((struct rx_counter_frame *)
  2502.                     search_state.counter_stack->sp)
  2503.                  : 0);
  2504.                 struct rx_counter_frame * cf;
  2505.                 PUSH (search_state.counter_stack,
  2506.                   sizeof (struct rx_counter_frame));
  2507.                 cf = ((struct rx_counter_frame *)
  2508.                   search_state.counter_stack->sp);
  2509.                 cf->tag = re_se_iter;
  2510.                 cf->val = 0;
  2511.                 cf->inherited_from = 0;
  2512.                 cf->cdr = old_cf;
  2513.                 break;
  2514.               }
  2515.             case re_se_fail:
  2516.               goto test_do_return;
  2517.             case re_se_begbuf:
  2518.               if (!AT_STRINGS_BEG ())
  2519.                 goto test_do_return;
  2520.               break;
  2521.             case re_se_endbuf:
  2522.               if (!AT_STRINGS_END ())
  2523.                 goto test_do_return;
  2524.               break;
  2525.             case re_se_wordbeg:
  2526.               if (   LETTER_P (&search_state.test_pos, 1)
  2527.                   && (   AT_STRINGS_BEG()
  2528.                   || !LETTER_P (&search_state.test_pos, 0)))
  2529.                 break;
  2530.               else
  2531.                 goto test_do_return;
  2532.             case re_se_wordend:
  2533.               if (   !AT_STRINGS_BEG ()
  2534.                   && LETTER_P (&search_state.test_pos, 0)
  2535.                   && (AT_STRINGS_END ()
  2536.                   || !LETTER_P (&search_state.test_pos, 1)))
  2537.                 break;
  2538.               else
  2539.                 goto test_do_return;
  2540.             case re_se_wordbound:
  2541.               if (AT_WORD_BOUNDARY (&search_state.test_pos))
  2542.                 break;
  2543.               else
  2544.                 goto test_do_return;
  2545.             case re_se_notwordbound:
  2546.               if (!AT_WORD_BOUNDARY (&search_state.test_pos))
  2547.                 break;
  2548.               else
  2549.                 goto test_do_return;
  2550.             case re_se_hat:
  2551.               if (AT_STRINGS_BEG ())
  2552.                 {
  2553.                   if (rxb->not_bol)
  2554.                 goto test_do_return;
  2555.                   else
  2556.                 break;
  2557.                 }
  2558.               else
  2559.                 {
  2560.                   char pos_c = *search_state.test_pos.pos;
  2561.                   if (   (SRCH_TRANSLATE (pos_c)
  2562.                       == SRCH_TRANSLATE('\n'))
  2563.                   && rxb->newline_anchor)
  2564.                 break;
  2565.                   else
  2566.                 goto test_do_return;
  2567.                 }
  2568.             case re_se_dollar:
  2569.               if (AT_STRINGS_END ())
  2570.                 {
  2571.                   if (rxb->not_eol)
  2572.                 goto test_do_return;
  2573.                   else
  2574.                 break;
  2575.                 }
  2576.               else
  2577.                 {
  2578.                   if (   (   SRCH_TRANSLATE (fetch_char
  2579.                             (&search_state.test_pos, 1,
  2580.                              app_closure, stop))
  2581.                       == SRCH_TRANSLATE ('\n'))
  2582.                   && rxb->newline_anchor)
  2583.                 break;
  2584.                   else
  2585.                 goto test_do_return;
  2586.                 }
  2587.               
  2588.             case re_se_try:
  2589.               /* This is the first side effect in every
  2590.                * expression.
  2591.                *
  2592.                *  FOR NO GOOD REASON...get rid of it...
  2593.                */
  2594.               break;
  2595.               
  2596.             case re_se_pushpos:
  2597.               {
  2598.                 int urhere =
  2599.                   ((int)(search_state.test_pos.pos
  2600.                      - search_state.test_pos.string)
  2601.                    + search_state.test_pos.offset);
  2602.                 struct rx_counter_frame * old_cf
  2603.                   = (search_state.counter_stack
  2604.                  ? ((struct rx_counter_frame *)
  2605.                     search_state.counter_stack->sp)
  2606.                  : 0);
  2607.                 struct rx_counter_frame * cf;
  2608.                 PUSH(search_state.counter_stack,
  2609.                  sizeof (struct rx_counter_frame));
  2610.                 cf = ((struct rx_counter_frame *)
  2611.                   search_state.counter_stack->sp);
  2612.                 cf->tag = re_se_pushpos;
  2613.                 cf->val = urhere;
  2614.                 cf->inherited_from = 0;
  2615.                 cf->cdr = old_cf;
  2616.                 break;
  2617.               }
  2618.               
  2619.             case re_se_chkpos:
  2620.               {
  2621.                 int urhere =
  2622.                   ((int)(search_state.test_pos.pos
  2623.                      - search_state.test_pos.string)
  2624.                    + search_state.test_pos.offset);
  2625.                 struct rx_counter_frame * cf
  2626.                   = ((struct rx_counter_frame *)
  2627.                  search_state.counter_stack->sp);
  2628.                 if (cf->val == urhere)
  2629.                   goto test_do_return;
  2630.                 cf->val = urhere;
  2631.                 break;
  2632.               }
  2633.               break;
  2634.               
  2635.             case re_se_poppos:
  2636.               POP(search_state.counter_stack,
  2637.                   sizeof (struct rx_counter_frame));
  2638.               break;
  2639.               
  2640.               
  2641.             case re_se_at_dot:
  2642.             case re_se_syntax:
  2643.             case re_se_not_syntax:
  2644. #ifdef emacs
  2645.               this release lacks emacs support;
  2646.               (coming soon);
  2647. #endif
  2648.               break;
  2649.             case re_se_win:
  2650.             case re_se_lparen:
  2651.             case re_se_rparen:
  2652.             case re_se_backref:
  2653.             case re_se_iter:
  2654.             case re_se_end_iter:
  2655.             case re_se_tv:
  2656.             case re_floogle_flap:
  2657.               search_state.ret_val = 0;
  2658.               goto test_do_return;
  2659.             }
  2660.             }
  2661.           else
  2662.             {
  2663. #ifdef RX_DEBUG
  2664.               if (rx_debug_trace)
  2665.             fprintf (stderr, "....%d %d>> %s %d %d\n", search_state.line_no,
  2666.                  search_state.backtrack_depth,
  2667.                  efnames2[rxb->se_params [effect].se],
  2668.                  rxb->se_params [effect].op1,
  2669.                  rxb->se_params [effect].op2);
  2670. #endif
  2671.               switch (rxb->se_params [effect].se)
  2672.             {
  2673.             case re_se_win:
  2674.               /* This side effect indicates that we've 
  2675.                * found a match, though not necessarily the 
  2676.                * best match.  This is a fancy assignment to 
  2677.                * register 0 unless the caller didn't 
  2678.                * care about registers.  In which case,
  2679.                * this stops the match.
  2680.                */
  2681.               {
  2682.                 int urhere =
  2683.                   ((int)(search_state.test_pos.pos
  2684.                      - search_state.test_pos.string)
  2685.                    + search_state.test_pos.offset);
  2686.                 
  2687.                 if (   (search_state.best_last_r < 0)
  2688.                 || (urhere + 1 > search_state.best_rparen[0]))
  2689.                   {
  2690.                 /* Record the best known and keep
  2691.                  * looking.
  2692.                  */
  2693.                 int x;
  2694.                 for (x = 0; x <= search_state.last_l; ++x)
  2695.                   search_state.best_lparen[x] = search_state.lparen[x];
  2696.                 search_state.best_last_l = search_state.last_l;
  2697.                 for (x = 0; x <= search_state.last_r; ++x)
  2698.                   search_state.best_rparen[x] = search_state.rparen[x];
  2699.                 search_state.best_rparen[0] = urhere + 1;
  2700.                 search_state.best_last_r = search_state.last_r;
  2701.                   }
  2702.                 /* If we're not reporting the match-length 
  2703.                  * or other register info, we need look no
  2704.                  * further.
  2705.                  */
  2706.                 if (search_state.first_found)
  2707.                   {
  2708.                 search_state.test_ret = rx_test_found_first;
  2709.                 goto test_do_return;
  2710.                   }
  2711.               }
  2712.               break;
  2713.             case re_se_lparen:
  2714.               {
  2715.                 int urhere =
  2716.                   ((int)(search_state.test_pos.pos
  2717.                      - search_state.test_pos.string)
  2718.                    + search_state.test_pos.offset);
  2719.                 
  2720.                 int reg = rxb->se_params [effect].op1;
  2721. #if 0
  2722.                 if (reg > search_state.last_l)
  2723. #endif
  2724.                   {
  2725.                 search_state.lparen[reg] = urhere + 1;
  2726.                 /* In addition to making this assignment,
  2727.                  * we now know that lower numbered regs
  2728.                  * that haven't already been assigned,
  2729.                  * won't be.  We make sure they're
  2730.                  * filled with -1, so they can be
  2731.                  * recognized as unassigned.
  2732.                  */
  2733.                 if (search_state.last_l < reg)
  2734.                   while (++search_state.last_l < reg)
  2735.                     search_state.lparen[search_state.last_l] = -1;
  2736.                   }
  2737.                 break;
  2738.               }
  2739.               
  2740.             case re_se_rparen:
  2741.               {
  2742.                 int urhere =
  2743.                   ((int)(search_state.test_pos.pos
  2744.                      - search_state.test_pos.string)
  2745.                    + search_state.test_pos.offset);
  2746.                 int reg = rxb->se_params [effect].op1;
  2747.                 search_state.rparen[reg] = urhere + 1;
  2748.                 if (search_state.last_r < reg)
  2749.                   {
  2750.                 while (++search_state.last_r < reg)
  2751.                   search_state.rparen[search_state.last_r]
  2752.                     = -1;
  2753.                   }
  2754.                 break;
  2755.               }
  2756.               
  2757.             case re_se_backref:
  2758.               {
  2759.                 int reg = rxb->se_params [effect].op1;
  2760.                 if (   reg > search_state.last_r
  2761.                 || search_state.rparen[reg] < 0)
  2762.                   goto test_do_return;
  2763.                 
  2764.                 {
  2765.                   int backref_status;
  2766.                 check_backreference:
  2767.                   backref_status
  2768.                 = back_check (&search_state.test_pos,
  2769.                           search_state.lparen[reg],
  2770.                           search_state.rparen[reg],
  2771.                           search_state.translate,
  2772.                           app_closure,
  2773.                           stop);
  2774.                   switch (backref_status)
  2775.                 {
  2776.                 case rx_back_check_continuation:
  2777.                   search_state.saved_reg = reg;
  2778.                   test_pc = rx_test_backreference_check;
  2779.                   goto test_return_continuation;
  2780.                 resume_continuation_2:
  2781.                   reg = search_state.saved_reg;
  2782.                   goto check_backreference;
  2783.                 case rx_back_check_fail:
  2784.                   /* Fail */
  2785.                   goto test_do_return;
  2786.                 case rx_back_check_pass:
  2787.                   /* pass --
  2788.                    * test_pos now advanced to last
  2789.                    * char matched by backref
  2790.                    */
  2791.                   break;
  2792.                 }
  2793.                 }
  2794.                 break;
  2795.               }
  2796.             case re_se_iter:
  2797.               {
  2798.                 struct rx_counter_frame * csp
  2799.                   = ((struct rx_counter_frame *)
  2800.                  search_state.counter_stack->sp);
  2801.                 if (csp->val == rxb->se_params[effect].op2)
  2802.                   goto test_do_return;
  2803.                 else
  2804.                   ++csp->val;
  2805.                 break;
  2806.               }
  2807.             case re_se_end_iter:
  2808.               {
  2809.                 struct rx_counter_frame * csp
  2810.                   = ((struct rx_counter_frame *)
  2811.                  search_state.counter_stack->sp);
  2812.                 if (csp->val < rxb->se_params[effect].op1)
  2813.                   goto test_do_return;
  2814.                 else
  2815.                   {
  2816.                 struct rx_counter_frame * source = csp;
  2817.                 while (source->inherited_from)
  2818.                   source = source->inherited_from;
  2819.                 if (!source || !source->cdr)
  2820.                   {
  2821.                     POP(search_state.counter_stack,
  2822.                     sizeof(struct rx_counter_frame));
  2823.                   }
  2824.                 else
  2825.                   {
  2826.                     source = source->cdr;
  2827.                     csp->val = source->val;
  2828.                     csp->tag = source->tag;
  2829.                     csp->cdr = 0;
  2830.                     csp->inherited_from = source;
  2831.                   }
  2832.                   }
  2833.                 break;
  2834.               }
  2835.             case re_se_tv:
  2836.               /* is a noop */
  2837.               break;
  2838.             case re_se_try:
  2839.             case re_se_pushback:
  2840.             case re_se_push0:
  2841.             case re_se_pushpos:
  2842.             case re_se_chkpos:
  2843.             case re_se_poppos:
  2844.             case re_se_at_dot:
  2845.             case re_se_syntax:
  2846.             case re_se_not_syntax:
  2847.             case re_se_begbuf:
  2848.             case re_se_hat:
  2849.             case re_se_wordbeg:
  2850.             case re_se_wordbound:
  2851.             case re_se_notwordbound:
  2852.             case re_se_wordend:
  2853.             case re_se_endbuf:
  2854.             case re_se_dollar:
  2855.             case re_se_fail:
  2856.             case re_floogle_flap:
  2857.               search_state.ret_val = 0;
  2858.               goto test_do_return;
  2859.             }
  2860.             }
  2861.           el = el->cdr;
  2862.         }
  2863.           /* Now the side effects are done,
  2864.            * so get the next instruction.
  2865.            * and move on.
  2866.            */
  2867.           search_state.ifr = &df->future_frame;
  2868.           goto restart;
  2869.         }
  2870.         
  2871.       case rx_backtrack_point:
  2872.         {
  2873.           /* A backtrack point indicates that we've reached a
  2874.            * non-determinism in the superstate NFA.  This is a
  2875.            * loop that exhaustively searches the possibilities.
  2876.            *
  2877.            * A backtracking strategy is used.  We keep track of what
  2878.            * registers are valid so we can erase side effects.
  2879.            *
  2880.            * First, make sure there is some stack space to hold 
  2881.            * our state.
  2882.            */
  2883.           
  2884.           struct rx_backtrack_frame * bf;
  2885.           
  2886.           PUSH(search_state.backtrack_stack,
  2887.            search_state.backtrack_frame_bytes);
  2888. #ifdef RX_DEBUG
  2889.           ++search_state.backtrack_depth;
  2890. #endif
  2891.           
  2892.           bf = ((struct rx_backtrack_frame *)
  2893.             search_state.backtrack_stack->sp);
  2894.           {
  2895.         bf->stk_super = search_state.super;
  2896.         /* We prevent the current superstate from being
  2897.          * deleted from the superstate cache.
  2898.          */
  2899.         rx_lock_superstate (&rxb->rx, search_state.super);
  2900. #ifdef RX_DEBUG
  2901.         bf->stk_search_state.line_no = search_state.line_no;
  2902. #endif
  2903.         bf->stk_c = search_state.c;
  2904.         bf->stk_test_pos = search_state.test_pos;
  2905.         bf->stk_last_l = search_state.last_l;
  2906.         bf->stk_last_r = search_state.last_r;
  2907.         bf->df = ((struct rx_super_edge *)
  2908.               search_state.ifr->data_2)->options;
  2909.         bf->first_df = bf->df;
  2910.         bf->counter_stack_sp = (search_state.counter_stack
  2911.                     ? search_state.counter_stack->sp
  2912.                     : 0);
  2913.         bf->stk_test_ret = search_state.test_ret;
  2914.         if (rxb->match_regs_on_stack)
  2915.           {
  2916.             int x;
  2917.             regoff_t * stk =
  2918.               (regoff_t *)((char *)bf + sizeof (*bf));
  2919.             for (x = 0; x <= search_state.last_l; ++x)
  2920.               stk[x] = search_state.lparen[x];
  2921.             stk += x;
  2922.             for (x = 0; x <= search_state.last_r; ++x)
  2923.               stk[x] = search_state.rparen[x];
  2924.           }
  2925.           }
  2926.           
  2927.           /* Here is a while loop whose body is mainly a function
  2928.            * call and some code to handle a return from that
  2929.            * function.
  2930.            *
  2931.            * From here on for the rest of `case backtrack_point' it
  2932.            * is unsafe to assume that the search_state copies of 
  2933.            * variables saved on the backtracking stack are valid
  2934.            * -- so read their values from the backtracking stack.
  2935.            *
  2936.            * This lets us use one generation fewer stack saves in
  2937.            * the call-graph of a search.
  2938.            */
  2939.           
  2940.         while_non_det_options:
  2941. #ifdef RX_DEBUG
  2942.           ++search_state.lines_found;
  2943.           if (rx_debug_trace)
  2944.         fprintf (stderr, "@@@ %d calls %d @@@\n",
  2945.              search_state.line_no, search_state.lines_found);
  2946.           
  2947.           search_state.line_no = search_state.lines_found;
  2948. #endif
  2949.           
  2950.           if (bf->df->next_same_super_edge[0] == bf->first_df)
  2951.         {
  2952.           /* This is a tail-call optimization -- we don't recurse
  2953.            * for the last of the possible futures.
  2954.            */
  2955.           search_state.ifr = (bf->df->effects
  2956.                       ? &bf->df->side_effects_frame
  2957.                       : &bf->df->future_frame);
  2958.           
  2959.           rx_unlock_superstate (&rxb->rx, search_state.super);
  2960.           POP(search_state.backtrack_stack,
  2961.               search_state.backtrack_frame_bytes);
  2962. #ifdef RX_DEBUG
  2963.           --search_state.backtrack_depth;
  2964. #endif
  2965.           goto restart;
  2966.         }
  2967.           else
  2968.         {
  2969.           if (search_state.counter_stack)
  2970.             {
  2971.               struct rx_counter_frame * old_cf
  2972.             = ((struct rx_counter_frame *)search_state.counter_stack->sp);
  2973.               struct rx_counter_frame * cf;
  2974.               PUSH(search_state.counter_stack, sizeof (struct rx_counter_frame));
  2975.               cf = ((struct rx_counter_frame *)search_state.counter_stack->sp);
  2976.               cf->tag = old_cf->tag;
  2977.               cf->val = old_cf->val;
  2978.               cf->inherited_from = old_cf;
  2979.               cf->cdr = 0;
  2980.             }            
  2981.           /* `Call' this test-match block */
  2982.           search_state.ifr = (bf->df->effects
  2983.                       ? &bf->df->side_effects_frame
  2984.                       : &bf->df->future_frame);
  2985.           goto recurse_test_match;
  2986.         }
  2987.           
  2988.           /* Returns in this block are accomplished by
  2989.            * goto test_do_return.  There are two cases.
  2990.            * If there is some search-stack left,
  2991.            * then it is a return from a `recursive' call.
  2992.            * If there is no search-stack left, then
  2993.            * we should return to the fastmap/search loop.
  2994.            */
  2995.           
  2996.         test_do_return:
  2997.           
  2998.           if (!search_state.backtrack_stack)
  2999.         {
  3000. #ifdef RX_DEBUG
  3001.           if (rx_debug_trace)
  3002.             fprintf (stderr, "!!! %d bails returning %d !!!\n",
  3003.                  search_state.line_no, search_state.test_ret);
  3004. #endif
  3005.           
  3006.           /* No more search-stack -- this test is done. */
  3007.           if (search_state.test_ret)
  3008.             goto return_from_test_match;
  3009.           else
  3010.             goto error_in_testing_match;
  3011.         }
  3012.           
  3013.           /* Returning from a recursive call to 
  3014.            * the test match block:
  3015.            */
  3016.           
  3017.           bf = ((struct rx_backtrack_frame *)
  3018.             search_state.backtrack_stack->sp);
  3019. #ifdef RX_DEBUG
  3020.           if (rx_debug_trace)
  3021.         fprintf (stderr, "+++ %d returns %d (to %d)+++\n",
  3022.              search_state.line_no,
  3023.              search_state.test_ret,
  3024.              bf->stk_search_state.line_no);
  3025. #endif
  3026.           
  3027.           while (search_state.counter_stack
  3028.              && (!bf->counter_stack_sp
  3029.              || (bf->counter_stack_sp
  3030.                  != search_state.counter_stack->sp)))
  3031.         {
  3032.           POP(search_state.counter_stack,
  3033.               sizeof (struct rx_counter_frame));
  3034.         }
  3035.           
  3036.           if (search_state.test_ret == rx_test_error)
  3037.         {
  3038.           POP (search_state.backtrack_stack,
  3039.                search_state.backtrack_frame_bytes);
  3040.           goto test_do_return;
  3041.         }
  3042.           
  3043.           /* If a non-longest match was found and that is good 
  3044.            * enough, return immediately.
  3045.            */
  3046.           if (   (search_state.test_ret == rx_test_found_first)
  3047.           && search_state.first_found)
  3048.         {
  3049.           rx_unlock_superstate (&rxb->rx, bf->stk_super);
  3050.           POP (search_state.backtrack_stack,
  3051.                search_state.backtrack_frame_bytes);
  3052.           goto test_do_return;
  3053.         }
  3054.           
  3055.           search_state.test_ret = bf->stk_test_ret;
  3056.           search_state.last_l = bf->stk_last_l;
  3057.           search_state.last_r = bf->stk_last_r;
  3058.           bf->df = bf->df->next_same_super_edge[0];
  3059.           search_state.super = bf->stk_super;
  3060.           search_state.c = bf->stk_c;
  3061. #ifdef RX_DEBUG
  3062.           search_state.line_no = bf->stk_search_state.line_no;
  3063. #endif
  3064.           
  3065.           if (rxb->match_regs_on_stack)
  3066.         {
  3067.           int x;
  3068.           regoff_t * stk =
  3069.             (regoff_t *)((char *)bf + sizeof (*bf));
  3070.           for (x = 0; x <= search_state.last_l; ++x)
  3071.             search_state.lparen[x] = stk[x];
  3072.           stk += x;
  3073.           for (x = 0; x <= search_state.last_r; ++x)
  3074.             search_state.rparen[x] = stk[x];
  3075.         }
  3076.           
  3077.           {
  3078.         int x;
  3079.           try_burst_2:
  3080.         x = get_burst (&bf->stk_test_pos, app_closure, stop);
  3081.         switch (x)
  3082.           {
  3083.           case rx_get_burst_continuation:
  3084.             search_state.saved_bf = bf;
  3085.             test_pc = rx_test_backtrack_return;
  3086.             goto test_return_continuation;
  3087.           resume_continuation_3:
  3088.             bf = search_state.saved_bf;
  3089.             goto try_burst_2;
  3090.           case rx_get_burst_no_more:
  3091.             /* Since we've been here before, it is some kind of
  3092.              * error that we can't return.
  3093.              */
  3094.           case rx_get_burst_error:
  3095.             search_state.test_ret = rx_test_internal_error;
  3096.             goto test_do_return;
  3097.           case rx_get_burst_ok:
  3098.             break;
  3099.           }
  3100.           }
  3101.           search_state.test_pos = bf->stk_test_pos;
  3102.           goto while_non_det_options;
  3103.         }
  3104.         
  3105.         
  3106.       case rx_cache_miss:
  3107.         /* Because the superstate NFA is lazily constructed,
  3108.          * and in fact may erode from underneath us, we sometimes
  3109.          * have to construct the next instruction from the hard way.
  3110.          * This invokes one step in the lazy-conversion.
  3111.          */
  3112.         search_state.ifr = rx_handle_cache_miss (&rxb->rx,
  3113.                              search_state.super,
  3114.                              search_state.c,
  3115.                              search_state.ifr->data_2);
  3116.         if (!search_state.ifr)
  3117.           {
  3118.         search_state.test_ret = rx_test_internal_error;
  3119.         goto test_do_return;
  3120.           }
  3121.         goto restart;
  3122.         
  3123.       case rx_backtrack:
  3124.         /* RX_BACKTRACK means that we've reached the empty
  3125.          * superstate, indicating that match can't succeed
  3126.          * from this point.
  3127.          */
  3128.         goto test_do_return;
  3129.         
  3130.       case rx_next_char:
  3131.       case rx_error_inx:
  3132.       case rx_num_instructions:
  3133.         search_state.ret_val = 0;
  3134.         goto test_do_return;
  3135.       }
  3136.     goto pseudo_while_1;
  3137.       }
  3138.     
  3139.     /* Healthy exits from the test-match loop do a 
  3140.      * `goto return_from_test_match'   On the other hand, 
  3141.      * we might end up here.
  3142.      */
  3143.   error_in_testing_match:
  3144.     test_state = rx_test_error;
  3145.     goto test_returns_to_search;
  3146.     
  3147.     /***** fastmap/search loop body
  3148.      *          considering the results testing for a match
  3149.      */
  3150.     
  3151.   return_from_test_match:
  3152.     
  3153.     if (search_state.best_last_l >= 0)
  3154.       {
  3155.     if (regs && (regs->start != search_state.best_lparen))
  3156.       {
  3157.         bcopy (search_state.best_lparen, regs->start,
  3158.            regs->num_regs * sizeof (int));
  3159.         bcopy (search_state.best_rparen, regs->end,
  3160.            regs->num_regs * sizeof (int));
  3161.       }
  3162.     if (regs && !rxb->no_sub)
  3163.       {
  3164.         int q;
  3165.         int bound = (regs->num_regs > search_state.num_regs
  3166.              ? regs->num_regs
  3167.              : search_state.num_regs);
  3168.         regoff_t * s = regs->start;
  3169.         regoff_t * e = regs->end;
  3170.         for (q = search_state.best_last_l + 1;  q < bound; ++q)
  3171.           s[q] = e[q] = -1;
  3172.       }
  3173.     search_state.ret_val = search_state.best_lparen[0];
  3174.     test_state = rx_test_ok;
  3175.     goto test_returns_to_search;
  3176.       }
  3177.     else
  3178.       {
  3179.     test_state = rx_test_fail;
  3180.     goto test_returns_to_search;
  3181.       }
  3182.     
  3183.   test_return_continuation:
  3184.     search_state.test_match_resume_pt = test_pc;
  3185.     test_state = rx_test_continuation;
  3186.     goto test_returns_to_search;
  3187.   }
  3188. }
  3189.  
  3190.  
  3191.  
  3192. #endif /* RX_WANT_RX_DEFS */
  3193.  
  3194.  
  3195.  
  3196. #else /* RX_WANT_SE_DEFS */
  3197.   /* Integers are used to represent side effects.
  3198.    *
  3199.    * Simple side effects are given negative integer names by these enums.
  3200.    * 
  3201.    * Non-negative names are reserved for complex effects.
  3202.    *
  3203.    * Complex effects are those that take arguments.  For example, 
  3204.    * a register assignment associated with a group is complex because
  3205.    * it requires an argument to tell which group is being matched.
  3206.    * 
  3207.    * The integer name of a complex effect is an index into rxb->se_params.
  3208.    */
  3209.  
  3210.   RX_DEF_SE(1, re_se_try, = -1)        /* Epsilon from start state */
  3211.  
  3212.   RX_DEF_SE(0, re_se_pushback, = re_se_try - 1)
  3213.   RX_DEF_SE(0, re_se_push0, = re_se_pushback -1)
  3214.   RX_DEF_SE(0, re_se_pushpos, = re_se_push0 - 1)
  3215.   RX_DEF_SE(0, re_se_chkpos, = re_se_pushpos -1)
  3216.   RX_DEF_SE(0, re_se_poppos, = re_se_chkpos - 1)
  3217.  
  3218.   RX_DEF_SE(1, re_se_at_dot, = re_se_poppos - 1)    /* Emacs only */
  3219.   RX_DEF_SE(0, re_se_syntax, = re_se_at_dot - 1) /* Emacs only */
  3220.   RX_DEF_SE(0, re_se_not_syntax, = re_se_syntax - 1) /* Emacs only */
  3221.  
  3222.   RX_DEF_SE(1, re_se_begbuf, = re_se_not_syntax - 1) /* match beginning of buffer */
  3223.   RX_DEF_SE(1, re_se_hat, = re_se_begbuf - 1) /* match beginning of line */
  3224.  
  3225.   RX_DEF_SE(1, re_se_wordbeg, = re_se_hat - 1) 
  3226.   RX_DEF_SE(1, re_se_wordbound, = re_se_wordbeg - 1)
  3227.   RX_DEF_SE(1, re_se_notwordbound, = re_se_wordbound - 1)
  3228.  
  3229.   RX_DEF_SE(1, re_se_wordend, = re_se_notwordbound - 1)
  3230.   RX_DEF_SE(1, re_se_endbuf, = re_se_wordend - 1)
  3231.  
  3232.   /* This fails except at the end of a line. 
  3233.    * It deserves to go here since it is typicly one of the last steps 
  3234.    * in a match.
  3235.    */
  3236.   RX_DEF_SE(1, re_se_dollar, = re_se_endbuf - 1)
  3237.  
  3238.   /* Simple effects: */
  3239.   RX_DEF_SE(1, re_se_fail, = re_se_dollar - 1)
  3240.  
  3241.   /* Complex effects.  These are used in the 'se' field of 
  3242.    * a struct re_se_params.  Indexes into the se array
  3243.    * are stored as instructions on nfa edges.
  3244.    */
  3245.   RX_DEF_CPLX_SE(1, re_se_win, = 0)
  3246.   RX_DEF_CPLX_SE(1, re_se_lparen, = re_se_win + 1)
  3247.   RX_DEF_CPLX_SE(1, re_se_rparen, = re_se_lparen + 1)
  3248.   RX_DEF_CPLX_SE(0, re_se_backref, = re_se_rparen + 1)
  3249.   RX_DEF_CPLX_SE(0, re_se_iter, = re_se_backref + 1) 
  3250.   RX_DEF_CPLX_SE(0, re_se_end_iter, = re_se_iter + 1)
  3251.   RX_DEF_CPLX_SE(0, re_se_tv, = re_se_end_iter + 1)
  3252.  
  3253. #endif
  3254.  
  3255. #endif
  3256.